-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAggressive Cow.java
More file actions
50 lines (41 loc) · 931 Bytes
/
Aggressive Cow.java
File metadata and controls
50 lines (41 loc) · 931 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
package lec1 ;
import java.util.*;
public class Summ {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in) ;
int n =scn.nextInt();
int m = scn.nextInt();
int[] arr = new int[n] ;
for(int i=0;i<m ;i++)
{
arr[i]=scn.nextInt();
}
Arrays.sort(arr);
int low=arr[0];
int high=arr[n-1] ;
int mid=0 ;
while(low<=high)
{
mid = (low+high)/2 ;
if(ispossible(arr,mid,m))
{
low=mid+1 ;
}
else
high=mid-1 ;
}
System.out.println(mid);
}
private static boolean ispossible(int[] arr,int mid,int m) {
int p=arr[0] ;
int l=1 ;
for(int i=0;i<arr.length;i++)
{
if(arr[i]-p>=mid)
l++ ;
if(m==l)
return true ;
}
return false ;
}
}