-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAGGRCOW_Aggressive_cows.java
More file actions
65 lines (46 loc) · 1.42 KB
/
AGGRCOW_Aggressive_cows.java
File metadata and controls
65 lines (46 loc) · 1.42 KB
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
import java.util.Arrays;
import java.util.Scanner;
public class AGGRCOW_Aggressive_cows {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int no_Stalls = sc.nextInt();
int no_cows = sc.nextInt();
int[] stall = new int[no_Stalls];
for (int i = 0; i < stall.length; i++) {
stall[i] = sc.nextInt();
}
Arrays.sort(stall);
System.out.println(LargeMinDistance(stall, no_cows));
}
public static int LargeMinDistance(int[] stall, int noc) {
int low = 0;
int high = stall[stall.length - 1] - stall[0];// 9-1
int ans = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (possiable(stall, mid, noc) == true) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}
public static boolean possiable(int[] stall, int mid, int noc) {
int cows = 1;
int position = stall[0];
int i = 1;
while (i < stall.length) {
if (stall[i] - position >= mid) {
cows++;
position = stall[i];
}
if (cows == noc) {
return true;
}
i++;
}
return false;
}
}