-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2616. Minimize the Maximum Difference of Pairs.java
60 lines (60 loc) · 2.12 KB
/
2616. Minimize the Maximum Difference of Pairs.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
class Solution {
public int minimizeMax(int[] nums, int p) {
if (p == 0) return 0;
/**
* simply binary search problem
* as MIN MAX problem
*
* same like the aggresive cows - modified version
*/
// sort the array
Arrays.sort(nums); // as we need to perform binary searchh
// the maximum pair difference will be the smallest element
// and largest element in the array
// so my serach space is (0.....(lar-small))
int low = 0;
int high = (nums[nums.length - 1] - nums[0]);
// iterate on the search space and find the sweet spot of having P pairs
int ans=0;
while (low <= high) {
int possibleDiff = (low + (high - low) / 2);
// check my possibleDiff has atleast P paris
boolean hasPPairs = countOfPairsAtleastP(nums, possibleDiff, p);
// if you found ; then try to minimise the search space by HIGH = MID - 1
if (hasPPairs) {
high = possibleDiff - 1;
ans=possibleDiff;
} else {
low = possibleDiff + 1;
}
// if not increase the lower bound
}
// ans will be the minimal maximum difference
return ans;
}
private boolean countOfPairsAtleastP(int[] nums, int diff, int p) {
int countPairs = 0;
int i = 0;
while (i < nums.length - 1) {
if (absoluteDifference(nums[i], nums[i + 1]) <= diff) {
countPairs++;
i += 2;
} else {
i++;
}
if (countPairs == p) {
return true;
}
}
return false;
}
// return the absolute diff of a and b elem
private int absoluteDifference(int a, int b) {
return Math.abs(a - b);
}
}