-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution39.java
119 lines (97 loc) · 3.31 KB
/
Solution39.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
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.
You can find a slight modification of the problem here.
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers.
Example 1:
Input:
K = 2, N = 4
Arr[] = {1, 5, 8, 10}
Output:
5
Explanation:
The array can be modified as
{1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.
The difference between
the largest and the smallest is 8-3 = 5.
Example 2:
Input:
K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
Output:
11
Explanation:
The array can be modified as
{3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.
The difference between
the largest and the smallest is 17-6 = 11.
Your Task:
You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n, and k as input parameters and returns an integer denoting the minimum difference.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints
1 ≤ K ≤ 109
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 109
*/
class Solution {
static class Pair {
int first, second;
Pair(int x, int y) {
first = x;
second = y;
}
}
int getMinDiff(int[] arr, int n, int k) {
ArrayList<Pair> v = new ArrayList<Pair>();
int[] taken = new int[n];
// we will store all possible heights in a vector
for (int i = 0; i < n; i++) {
if (arr[i] - k >= 0) {
v.add(new Pair(arr[i] - k, i));
}
v.add(new Pair(arr[i] + k, i));
}
Collections.sort(v, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2) {
if (p1.first == p2.first) return p1.second - p2.second;
return p1.first - p2.first;
}
});
int elements_in_range = 0;
int left = 0;
int right = 0;
// By two pointer we will traverse v and whenever we will get a range
// in which all towers are included, we will update the answer.
while (elements_in_range < n && right < v.size()) {
if (taken[v.get(right).second] == 0) {
elements_in_range++;
}
taken[v.get(right).second]++;
right++;
}
int ans = v.get(right - 1).first - v.get(left).first;
while (right < v.size()) {
if (taken[v.get(left).second] == 1) {
elements_in_range--;
}
taken[v.get(left).second]--;
left++;
while (elements_in_range < n && right < v.size()) {
if (taken[v.get(right).second] == 0) {
elements_in_range++;
}
taken[v.get(right).second]++;
right++;
}
if (elements_in_range == n) {
ans = Math.min(ans, v.get(right - 1).first - v.get(left).first);
} else
break;
}
return ans;
}
}