Hey!! We can use binary search in the check function too right. We first of all sort the stalls. Then we always keep cow1 in stall1 and then proceed by finding the lower_bound in array of stalls for last_stall+dist.

If the lower_bound doesn't point to the end we know we can accomodate one more cow.And then check how many cows can we accomodate with minimum distance dist and if it's greater than or equal to the cows present c then it would return true but if noot return false.

Then the algo would work in Time Complexity O((log n)^2).

But it's showing Time Limit Exceeded.

My whole code is...

#include <bits/stdc++.h>using namespace std;int N,C;vector<long long> v;bool chk(long long dist){