The Trick That Made Me Stop Fearing "Maximize the Minimum" Problems (Binary Search on the Answer)
Difficulty: beginner to intermediate. Reading time: about 6 minutes.
Binary search halving the search range at every step. Image: "Binary Search Depiction" by Wikimedia Commons (CC BY-SA).
Have you ever opened a problem and seen this?
"Maximize the minimum distance between any two cows."
You stare at it. You think about DP, greedy, sorting, and nothing clicks. Then someone in the comments writes "just binary search on the answer" and you feel like everyone knew a secret except you.
This post is that secret, explained so you can use it today.
The big idea
Normally we binary search in an array. Here we binary search on the answer itself.
Instead of asking "What is the best answer?" (hard), ask:
"Is answer X achievable?" (often easy!)
If the answer to that yes/no question is monotonic (all "yes" up to some point, then all "no", or vice versa), you can binary search over X.
X: 1 2 3 4 5 6 7 8 9
check: Y Y Y Y Y N N N N
^
the best answer
Example: Aggressive Cows
You have
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Step 1: Flip the question. Can we place all
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Step 2: Check greedily. Sort the stalls. Put the first cow in the first stall, then put each next cow in the first stall at least
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Step 3: Check monotonicity. If
Unable to parse markup [type=CF_MATHJAX]
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<long long> a;
bool can(long long d) {
int cnt = 1;
long long last = a[0];
for (int i = 1; i < n; i++) {
if (a[i] — last >= d) {
cnt++;
last = a[i];
}
}
return cnt >= k;
}
int main() {
cin >> n >> k;
a.resize(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
long long lo = 1, hi = a.back() — a.front(), ans = 0;
while (lo <= hi) {
long long mid = lo + (hi — lo) / 2;
if (can(mid)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid — 1;
}
}
cout << ans << "\n";
}
Complexity:
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
Unable to parse markup [type=CF_MATHJAX]
The 3-question checklist
Before using this trick, ask yourself:
- Is the answer a number in a bounded range? (time, distance, size, cost...)
- Can I write a
check(x)that returns yes/no? - Is
checkmonotonic? If x works, does x-1 (or x+1) also work?
If all three are yes, you've probably found a binary search problem.
Common mistakes (I've made all of them)
- Infinite loops. Use
mid = lo + (hi — lo) / 2and be careful whether you movelo = mid + 1orhi = mid. - Overflow. Upper bounds like
$ breakUnable to parse markup [type=CF_MATHJAX]
int. Uselong long. - Wrong bounds. If
hiis too small, you silently get the wrong answer. When in doubt, make it bigger. It only costs a few extra iterations. - Non-monotonic checks. If your
checkisn't monotonic, binary search will lie to you. Test with small cases.
Practice problems
- 1201C. Maximum Median
- 1117C. Magic Ship
- Problemset: filter by the tag
binary searchand sort by rating
Final thoughts
The scariest-looking optimization problems often become easy once you stop asking "what's the best answer?" and start asking "is this answer possible?" That one shift in thinking took me from stuck to solving a whole new class of problems.
Your turn: what's the coolest problem you've solved with this trick? Drop it in the comments, and if this helped, an upvote helps others find it!



