bool isPossibleSolution(vector<int> &stalls, int k, int mid){
int numCow = 1;
int initialPos = stalls[0];
for(int i=1; i<stalls.size(); i++){
if(stalls[i]-initialPos>=mid){
initialPos=stalls[i];
numCow++;
}
if(numCow>=k) return true;
}
return false;
}
int checkPossibleAnswer(vector<int> &stalls, int k, int s, int e, int ans){
if(s>e){
return ans;
}
int mid = s+(e-s)/2;
if(isPossibleSolution(stalls, k, mid)){
ans = mid;
checkPossibleAnswer(stalls, k, mid+1, e, ans);
}else{
checkPossibleAnswer(stalls, k, s, mid-1, ans);
}
}
int aggressiveCows(vector<int> &stalls, int k)
{
// Write your code here.
sort(stalls.begin(), stalls.end());
int s = 1;
int e = stalls[stalls.size()-1] - stalls[0];
return checkPossibleAnswer(stalls, k, s, e, -1);
}