What is the problem in this code?

#include
using namespace std;

bool canPlaceCows(int stalls[], int n, int c, int min_Step)
{
int last_cow = stalls[0];
// place the first cows in the first stall
int cnt = 1;
for (int i = 1; i < n; i++)
{
if (stalls[i] - last_cow >= min_Step)
{
last_cow = stalls[i];
cnt++;
if (cnt == c)
{
return true;
}
}
}
return false;
}

int main()
{
int n, cows;

cin >> n >> cows;
int stalls[n];
for (int i = 0; i < n; i++)
{
    cin >> stalls[i];
}

int s = 0;

int e = stalls[n - 1];
int ans = 0;

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

    bool cowsRakhPaye = canPlaceCows(stalls, n, cows, mid);
    if (cowsRakhPaye)
    {
        ans = mid;
        s = mid + 1;
    }
    else
    {
        e = mid - 1;
    }
}

cout << ans;
return 0;

}

Hey @ayushg.gupta24 I would request you to share your code using ide.codingblocks.com

So that I can debug your code.

Hey @ayushg.gupta24 there are only minute problems in your code
Firstly use int I=0 in your for loop in canplacecow() function.
Secondly, you should sort your array also as it’s not mentioned in the question that array will be sorted or not.
Thirdly, your int e in main function should be a[e] -a[0]
Fourthly, keep input and output format into consideration or else you will fail test cases.
Hope I helped you :grinning: keep coding :+1:

its worked i only to short the array. Thankyou soo much…

If you liked my service, do give this post a like and will be sharing feedback link soon with you :grinning: