-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaggressivecow.cpp
43 lines (40 loc) · 965 Bytes
/
aggressivecow.cpp
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
#include<bits/stdc++.h>
using namespace std;
bool isPossible(vector<int> &stalls, int k , int mid){
int cowCount =1;
int lstPos = stalls[0];
for(int i = 0 ; i< stalls.size() ; i++){
if(stalls[i]- lstPos >= mid){
cowCount++;
if(cowCount == k){
return true;
}
lstPos = stalls[i];
}
}
return false;
}
int aggressiveCows(vector<int> &stalls, int k)
{
// Write your code here.
sort(stalls.begin() , stalls.end());
int s =0;
int n = stalls.size();
int maxi = -1;
for(int i = 0; i< n ; i++){
maxi = max(maxi , stalls[i]);
};
int e =maxi;
int ans =-1;
int mid = s +(e-s)/2;
while(s<=e){
if(isPossible(stalls , k , mid)){
ans = mid ;
s = mid + 1;
}else{
e = mid -1 ;
}
mid = s +(e-s)/2;
}
return ans;
}