I am getting wrong answer in all test cases and even in the sample case despite using the code taught in video?

#include
using namespace std;
bool canplacecows(int arr[],int n,int c,int mid)
{
int cnt=0;
int prev=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]-prev>=mid)
{
prev=arr[i];
cnt++;
if(cnt==c)
{
return true;
}
}
}
return false;
}
int main() {
int n,c;
int arr[1000000];
cin>>n>>c;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int s=0;
int ans=0;
int e=arr[n-1]-arr[0];
while(s<=e)
{

	int mid=(s+e)/2;
	bool cowsrakhpaye=canplacecows(arr,n,c,mid);
	if(cowsrakhpaye)
	{
		ans=mid;
		s=mid+1;
	}
	else{
		e=mid-1;
	}
}
cout<<ans;
return 0;

}

Hello @mehulbhandari358,

  1. You have not considered the constraints properly.

  2. Wrong value of e.

I have corrected your code:

Hope, this would help.
Give a like if you are satisfied.

e is the maximum separation right? so that is first stall - last stall??

Thanks for correcting!

The corrected code is also giving wrong answer??

Hello @mehulbhandari358,

This is because you have not sorted the array.
And sorted sequence is essential for binary search problems.

Hope, this would help.