Why ans is 0 < Aggresive Cow>

#include
#include

using namespace std;

bool CanPlaceCows(int Stalls[], int n, int cows, int minSep)
{
int LastCow =Stalls[0];

int count=1;

for (int i = 1; i < n; i++)
{
    if (Stalls[i]-LastCow>=minSep)
    {
        LastCow=Stalls[i];
        count++;
        if (count==cows)
        {
            return true;
        }
    }
    
}
return false;

}

int main()
{
int n;
cin>>n;
int Stalls[n];
for (int i = 0; i < n; i++)
{
cin>>Stalls[i];
}
sort(Stalls,Stalls+n);
int cows;
cin>>cows;

int s=0;
int e= Stalls[n-1]-Stalls[0];
int ans=0;

while (s<=e)
{
    int mid = (s+e)/2;

    bool cowsRakhPayeKya = CanPlaceCows(Stalls,n,cows,mid);
    if (cowsRakhPayeKya)
    {
        ans = mid;
        s=mid+1;
    }
    else
    {
        e=mid-1;
    }
}
cout<<ans<<endl;
return 0;

}

Hey @rprahulpal03

    int n;
    cin>>n;
    int Stalls[n];
	int cows;//here
    cin>>cows;
    for (int i = 0; i < n; i++)
    {
        cin>>Stalls[i];
    }
    sort(Stalls,Stalls+n);
    // int cows;not here
    // cin>>cows;

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.