-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggressiveCow.java
More file actions
51 lines (45 loc) · 978 Bytes
/
AggressiveCow.java
File metadata and controls
51 lines (45 loc) · 978 Bytes
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
public class AggressiveCow {
public static int aggressiveCows(int[] A, int B) {
// code here
int n=A.length;
Arrays.sort(A);
int l=1;
int h=A[n-1]-A[0];
int ans=l;
while(l<=h)
{
int mid=l+(h-l)/2;
if(check(A,B,mid))
{
ans=mid;
//distance aaur v ho skta hai therfore go right
l=mid+1;
}
else
{
h=mid-1;
}
}
return ans;
}
//chechk function
public static boolean check(int[]A, int cow, int mid)
{
int n=A.length;
int count=1;
int j=0;
for(int i=1;i<n;i++)
{
if(A[i]-A[j]>=mid)
{
j=i;
count++;
}
if(count>=cow)
{
return true;
}
}
return false;
}
}