-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCow.cpp
59 lines (47 loc) · 978 Bytes
/
Cow.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
52
53
54
55
56
57
58
59
#include<iostream>
using namespace std;
// Stalls to Cows Minimum distance to Maximum Possible
int findKthPositive(int arr[], int n, int k)
{
int start=1, end, mid, ans;
end = arr[n-1]-arr[0];
while(start<=end)
{
mid = (start+end) / 2;
int count=1, pos=arr[0];
for(int i=1; i<n; i++)
{
if(pos+mid<=arr[i])
{
count++;
pos = arr[i];
}
}
if(count<k)
{
end = mid-1;
}
else
{
ans = mid;
start = mid+1;
}
}
cout<<"Position of Stalls: ";
return ans;
}
int main()
{
int arr[1000];
int n;
cout<<"Enter the number of Stalls: ";
cin>>n;
cout<<"Enter the Stalls Distance: ";
for(int i=0; i<n; i++)
cin>>arr[i];
int k;
cout<<"Aggressive Cows: ";
cin>>k;
cout<<findKthPositive(arr,n,k)<<endl;
return 0;
}