Back to All Blogs1 min read11 Sept 2026
DSA Patterns That Actually Matter · Part 3 of 5
Binary Search on the Answer: Search the Solution Space
“Use monotonic feasibility checks for optimization problems.”
Binary Search on the Answer
Binary search can work on any ordered solution space where a condition changes monotonically.
false false false false true true true
↑
first valid answer
Template
let low = minimumPossible;
let high = maximumPossible;
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (canDo(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
The Real Work
Define the search space, the feasibility check and monotonicity.
Warning
If the predicate is not monotonic, binary search cannot safely discard half of the search space.
Finished this article?
Mark it done to update your series progress.