can i get explanation or way for brute force method without binary search for better understanding.
thank you
can i get explanation or way for brute force method without binary search for better understanding.
thank you
bool check(int d, vector<int> a, int b) {
int n = a.size();
int cow = 1;
int prev = a[0];
for (int i = 1; i < n; i++) {
if (a[i] - prev >= d) {
prev = a[i];
cow++;
}
}
return cow >= b;
}
the brute force approach would be to run the check for each distance b, and get the largest value of b which satisfies the check function.
This check function() is same as the function used in binary search.
We exploit the search space in binary search because of its monotonicity.