Aggressive cow wrong answer on spoj

#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 ??

@Rj.25, please send your code using any online ide , due to some formatting some part of code are missing , you can use https://ide.codingblocks.com/

hi @Rj.25, your implementation is correct but you have made some small mistakes

  1. you are returning 0 in your main function inside your while loop
  2. you have to print every answer in a new line so use endl after answer

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

thanks a lot how to avoid this silly mitake

happens with everyone, just take your time to review your code and try some test cases to find the erro