getting wrong answer.
and pls explain to me the isvalid method by commenting out step by step that what is happening
Agresive cow problem
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); int temp = (int)N; long c = sc.nextInt(); long a[] = new long[temp]; for(int i =0;i<N;i++) { a[i]=sc.nextLong(); } Arrays.sort(a); System.out.println(cowDist(N,c,a)); } public static long cowDist(long n, long c, long[] a) { long s=0; long e=a[a.length-1]; long ans =0; while(s<=e) { long mid=(s+e)/2; if(isValid(a,n,c,ans)) { ans = mid; s=mid+1; }else { e=mid-1; } } return ans; } public static boolean isValid(long[] a, long n ,long c, long ans) { long pos=a[0]; long cow=1; for(int i =1;i<n;i++) { if(a[i]-pos>=ans) { pos=a[i]; cow++; if(cow==c) { return true; } } } if(cow<c) { return false; } return true; } }
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int c = sc.nextInt();
int a[] = new int[N];
for(int i =0;i<N;i++)
{
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println(cowDist(N,c,a));
}
public static int cowDist(int n, int c, int[] a) {
int s=0; int e=a[n-1];
int ans =0;
while(s<=e) {
int mid=(s+e)/2;
if(isValid(a,n,c,mid)) {//you were passing ans as the fourth argument in isValid function
ans = mid;
s=mid+1;
}else {
e=mid-1;
}
}
return ans;
}
public static boolean isValid(int[] a, int n ,int c, int ans) {
int pos=a[0];
int cow=1;
for(int i =1;i<n;i++) {
if(a[i]-pos>=ans) {
pos=a[i];
cow++;
if(cow==c) {
return true;
}
}
}
return false;//if we reach here then it means that we are not able to place c no of cows
}
}