-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaceTrack.java
More file actions
38 lines (31 loc) · 915 Bytes
/
RaceTrack.java
File metadata and controls
38 lines (31 loc) · 915 Bytes
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
package Searching;
public class RaceTrack {
static boolean isPossible(int[] a, int k, int dist) {
int kidsPlaces = 1;
int lastKid = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] - lastKid >= dist) {
kidsPlaces++;
lastKid = a[i];
}
}
return kidsPlaces >= k;
}
static int raceTrack(int[] a, int k) {
if (k > a.length)
return -1;
int st = 0, end = (int) 1e9, ans = -1;
while (st <= end) {
int mid = st + (end - st) / 2;
if (isPossible(a, k, mid)) { // Can k kids be placed such that no 2 kids have distance greater than mid.
ans = mid;
st = mid + 1;
} else {
end = mid - 1;
}
}
return ans;
}
public static void main(String[] args) {
}
}