-
Notifications
You must be signed in to change notification settings - Fork 2
/
minimize_heights_non_neg.cpp
64 lines (59 loc) · 1.69 KB
/
minimize_heights_non_neg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. After modifying, height should be a non-negative integer.
Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower.
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{3, 3, 6, 8}. The difference between
the largest and the smallest is 8-3 = 5.
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{6, 12, 9, 13, 17}. The difference between
the largest and the smallest is 17-6 = 11.
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int getMinDiff(int arr[], int n, int k) {
sort(arr, arr + n);
// if you add k to all elements / subtract k from all the elements
int result = arr[n - 1] - arr[0];
int min_val = arr[0] + k, max_val = arr[n - 1] - k;
for(int i = 1; i < n; i++) {
min_val = min(arr[0] + k, arr[i] - k);
max_val = max(arr[n - 1] -k, arr[i - 1] + k);
// avoid negative values
if(min_val < 0)
continue;
result = min(result, max_val - min_val);
}
return result;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> k;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
Solution ob;
auto ans = ob.getMinDiff(arr, n, k);
cout << ans << "\n";
}
return 0;
}