0Pricing
Competitive Programming Academy · Lesson

Binary Search on the Answer

Guess the result and check feasibility.

Guess, Then Verify

Sometimes you cannot compute the answer directly, but you can check a guess. Binary search on the answer turns hard optimization into easy checking.

# guess X, ask: is X feasible?

The Magic Property

It works when feasibility is monotonic: if a value works, every larger (or smaller) value also works. That ordering is what you search.

# feasible(X) true => feasible(X+1) true

All lessons in this course

  1. Classic Binary Search Without Bugs
  2. bisect_left and bisect_right
  3. First True: Predicate Binary Search
  4. Binary Search on the Answer
← Back to Competitive Programming Academy