Aggressive Cows Problem

#include
#include
using namespace std;

bool checkloop(int StallIndexes[], int mid,int NoStalls,int Nocows)

{int lastcow= StallIndexes[0];
int count=1;//error tha

for(int i=1;i<NoStalls; i++){
if((StallIndexes[i]-lastcow)>=mid){
lastcow=StallIndexes[i];
count++;
if(count==Nocows){
return true;
}

}

}
return false;

}

int main() {
int NoStalls;
cin>>NoStalls;
int Nocows;
cin>>Nocows;
int StallIndexes[Nocows];
for(int i=0;i<NoStalls;i++){
cin>>StallIndexes[i];
}
sort(StallIndexes,StallIndexes+NoStalls);
//we have sorted array now
//creating search space
int ans=-1;

int start=0;
int end= StallIndexes[NoStalls-1]-StallIndexes[0];

while(start<=end){
int mid= (start+end)/2;
bool Checkpossibility= checkloop( StallIndexes, mid, NoStalls, Nocows);

if(Checkpossibility){
    ans=mid;
    start=mid+1;
   
}
else{
    end=mid-1;
}

}
cout<< ans<<endl;
return 0;

}

I have edited your code… Try to submit it now…