-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathAggressive_cows.java
More file actions
63 lines (48 loc) · 1.41 KB
/
Aggressive_cows.java
File metadata and controls
63 lines (48 loc) · 1.41 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
// t – the number of test cases, then t test cases follows.
// * Line 1: Two space-separated integers: N and C
// * Lines 2..N+1: Line i+1 contains an integer stall location, xi
import java.util.*;
import java.lang.*;
// Aggressive_cows
public class Aggressive_cows{
public static boolean check(int arr[],int gap,int cows){
int count=1;
int last=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]-last>=gap){
++count;
last=arr[i];
}
}
if(count>=cows) return true;
return false;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int c=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
// for(int i:arr)
// System.out.print(i+" ");
int low=0,high=arr[arr.length-1]-arr[0];
int ans=0;
while(low<=high){
int mid=(low+high)/2;
if(check(arr,mid,c)){
low=mid+1;
ans=Math.max(ans,mid);
}else{
high=mid-1;
}
}
// System.out.println(check(arr,3,3));
System.out.println(ans);
}
}
}