-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggressiveCows.cpp
More file actions
55 lines (51 loc) · 1.03 KB
/
AggressiveCows.cpp
File metadata and controls
55 lines (51 loc) · 1.03 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isPossible(vector<int> &nums, int n, int c, int minDist) // mid = minDist here
{
int cows = 1;
int lastStallPos = nums[0];
for (int i = 1; i < n; i++)
{
if (nums[i] - lastStallPos >= minDist)
{
cows++;
lastStallPos = nums[i];
}
if (cows == c)
{
return true;
}
}
return false;
}
int CowCheck(vector<int> &nums, int n, int c)
{
sort(nums.begin(), nums.end());
int s = 1;
int e = nums[n - 1] - nums[0];
int ans = -1;
while (s <= e)
{
int mid = s + (e - s) / 2;
if (isPossible(nums, n, c, mid))
{
ans = mid;
s = mid + 1;
}
else
{
e = mid - 1;
}
}
return ans;
}
int main()
{
vector<int> nums = {1, 2, 8, 4, 9};
int n = 5;
int c = 3;
cout << "The answer is possible :: " << CowCheck(nums, n, c) << endl;
return 0;
}