forked from sunidhi2001/Hacktoberfest2022-accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimize_heights.java
66 lines (52 loc) · 1.89 KB
/
minimize_heights.java
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
65
//Minimize the Heights
//Problem statement
//Given an array arr[] denoting heights of N towers and a positive integer K.
//For each tower, you must perform exactly one of the following operations exactly once.
//Increase the height of the tower by K
//Decrease the height of the tower by K
//Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower.
//Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while (tc-- > 0) {
String[] inputLine;
inputLine = br.readLine().trim().split(" ");
int k = Integer.parseInt(inputLine[0]);
inputLine = br.readLine().trim().split(" ");
int n = Integer.parseInt(inputLine[0]);
int[] arr = new int[n];
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inputLine[i]);
}
int ans = new Solution().getMinDiff(arr, n, k);
System.out.println(ans);
}
}
}
class Solution {
int getMinDiff(int[] arr, int n, int k) {
int i;
int tmpSmall;
int tmpLarge;
int diff;
int smallest;
int largest;
Arrays.sort(arr);
tmpSmall = arr[0]+k;
tmpLarge = arr[n-1]-k;
diff = arr[n-1] - arr[0];
for(i=1;i<n;i++) {
smallest = Math.min(tmpSmall, arr[i]-k);
largest = Math.max(tmpLarge, arr[i-1]+k);
if(smallest<0)continue;
diff = Math.min(diff, largest - smallest);
}
return diff;
}
}