#include
#include
using namespace std;
int n,cows;
bool canplacecow(int stalls[],int n,int c,int min_sep){
int last_cow=stalls[0];
//place the first cow at first place
int cnt=1;
for(int i=1;i<n;i++){
if(stalls[i]-last_cow>=min_sep){
last_cow=stalls[i];
cnt++;
if (cnt==c){
return true;
}
}
}
return false;
}
int main()
{
//problem
int t;
cin>>t;
while(t–){
int n,cows;
cin>>n>>cows;
int stalls[100000];
for(int i=0;i<n;i++){
cin>>stalls[i];
}
sort(stalls,stalls+n);
// binary search algorithm
int s=0;
int e=stalls[n-1]-stalls[0];
int ans=0;
while(s<=e)
{
int mid= (s+e)/2;
bool cowrakh=canplacecow(stalls,n,cows,mid);
if (cowrakh){
ans=mid;
s=mid+1;
}
else{
e=mid-1;
}
}
cout<<ans;
return 0;
}
}
this is my final code for spoj when i submitted there it show wrong answer why ??