Here’s the link to the problem https://www.spoj.com/problems/AGGRCOW/

And here is my code:-

```
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,a,b) for(int i=a; i<b; i++)
bool f(long long x, long long c, const vector<long long>& d)
{
long long cnt = 1;
for(long long i=1; i<d.size() && cnt < c; i++)
{
if(d[i]-d[i-1] >= x) cnt++;
}
if(cnt == c) return true;
else return false;
}
int main()
{
int t;
cin >> t;
while(t--)
{
long long n, c;
cin >> n >> c;
vector<long long> d(n);
rep(i,0,n) cin >> d[i];
sort(d.begin(), d.end());
long long low = 0, high = d[n-1]-d[0];
while(low <= high)
{
long long mid = low + (high-low)/2;
if(f(mid,c,d)) low = mid+1;
else high = mid-1;
}
cout << low << "\n";
}
return 0;
}
```

I have tried the sample test case given with the problem and I get the correct output. To double check I picked up someone else’s code (here from Discuss itself) and tried matching my output for a test case with 1000 stalls and 648 cows, and again I got the correct answer. But on submitting my code I am getting WA. Please help me in figuring out the flaw in the code.