-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggressiveCows.cpp
51 lines (51 loc) · 1.07 KB
/
AggressiveCows.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
44
45
46
47
48
49
50
51
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool isPossible(vector <int> &stalls, int c, int mid)
{
int cowCount = 1;
int lastPos = stalls[0];
for (int i=0;i<stalls.size();i++)
{
if (stalls[i] - lastPos >= mid)
{
cowCount++;
if (cowCount == c)
{
return true;
}
lastPos = stalls[i];
}
}
return false;
}
int main()
{
vector <int> stalls = {4, 2, 1, 3, 6}; // Distance between stalls
sort(stalls.begin(),stalls.end());
int c = 2; // Number of Aggressive Cows
int start = 0;
int maxi = -1;
for (int i=0;i<stalls.size();i++)
{
maxi = max(maxi, stalls[i]);
}
int end = maxi;
int ans = -1;
int mid = (start+end)/2;
while (start<=end)
{
if (isPossible(stalls, c, mid))
{
ans = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
mid = start + (end-start)/2;
}
cout<<ans;
}