forked from striver79/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggressiveCows
40 lines (37 loc) · 1001 Bytes
/
aggressiveCows
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
#include <bits/stdc++.h>
using namespace std;
bool isPossible(int a[], int n, int cows, int minDist) {
int cntCows = 1;
int lastPlacedCow = a[0];
for(int i = 1;i<n;i++) {
if(a[i] - lastPlacedCow >= minDist) {
cntCows++;
lastPlacedCow = a[i];
}
}
if(cntCows >= cows) return true;
return false;
}
int main() {
int t;
cin >> t;
while(t--) {
int n, cows;
cin >> n >> cows;
int a[n];
for(int i = 0;i<n;i++) cin >> a[i];
sort(a,a+n);
int low = 1, high = a[n-1] - a[0];
while(low <= high) {
int mid = (low + high) >> 1;
if(isPossible(a,n,cows,mid)) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
cout << high << endl;
}
return 0;
}