Cant identify the problem in my code. Please help

#include
using namespace std;
bool canPlaceCows(long long a[],long long mid,long long n,long long c){
long long last_cow=a[0];
long long count=1;
for(int i=1;i<n;i++){
if(a[i]-last_cow>=mid){

		last_cow=a[i];
		count++;
	    if(count==c)
	    return true;
	}
}
return false;

}
int main(){
long long n,c;
cin>>n>>c;
long long a[n+1];
for(long long i=0;i<n;i++){
cin>>a[i];
}
long long s=0;
long long e=a[n-1]-a[0];
long long ans=0;
while(s<=e){
long long mid=(s+e)/2;

	if(canPlaceCows(a,mid,n,c)){
		ans=mid;

		s=mid+1;
	}
	else{
		e=mid-1;
	}
}
cout<<ans<<endl;
return 0;

}

hi @riagoel3999 sort the array

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.