Error in the code

#include<bits/stdc++.h>
using namespace std;
bool canplacecow(int stall[],int n,int c,int min)
{
int last=stall[0];
int count=1;
for(int i=1;i<n;i++)
{
if(stall[i]-last>=min)
{
last=stall[i];
count++;
if(count==c)
{
return true;
}
}
}
return false;
}
int main() {
int n,cow;
cin>>n>>cow;
int stall[1000];
for(int i=0;i<n;i++)
{
cin>>stall[i];
}
//binary search
sort(stall,stall+n);
int s=0;
int e=stall[n-1]-stall[0];
int ans=0;
int mid;
while(s<=e)
{
mid=(s+e)/2;
bool possible=canplacecow(stall ,n,cow,mid);
if(possible)
{
ans=mid;
s=mid+1;
}
else
{
e=mid-1;
}
}
cout<<ans<<endl;
return 0;
}

Hey @siddhant_samal please share your code using ide.codingblocks.com

Line number 31 make s=Stall[0]
In line 7 start I from 0 not from 1

i have tried that too but its not working…and my line 31 is corret as i am making monotonic search there …my code is doing passing some test cases

But it’s necessary to make your start point at stall[0] okay wait, https://github.com/DivonilLiquid/Data-Structure-Algorithm/blob/master/Searching%20%26%20Sorting/AggressiveCows.cpp here’s my code. I have added comments to. Just refer this one

sir ,I tried your code in ide to understand the code but it is showing wrong o/p ,its showing 00000

Yes you are right, i have printed answer in while loop, no issues you can have a look at this code https://ide.codingblocks.com/s/342302?_ga=2.192344234.1254462741.1601441754-1990619524.1596102109

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.