The Core Idea

Instead of searching within a sorted array, you’re searching over the range of possible answers to an optimization or feasibility problem. You assume there’s a monotonic predicate $P(x)$ on that range:

  • For all $x < A, P(x)$ is false
  • For all $x \geq A, P(x)$ is true

Your goal is to find the smallest (or largest) $A$ for which $P(A)$ flips to true.

 

When to Use It

  • Optimization problems where you want the minimum/maximum parameter satisfying some constraint.
  • You can quickly check feasibility for a candidate answer in, say, $O(f(n))$.
  • The predicate is monotonic (once true, stays true as you move right).

 

Pseudocode

Initialize your search bounds:

 

Loop while L < R:

 

Return R (or L) as the optimal answer:

Find smallest $x$ in $[L..R]$ with $P(x)==true$

 

It may also look like:

Last updated 11 May 2025, 18:45 +0500 . history