0PricingLogin
Competitive Programming Academy · Lesson

First True: Predicate Binary Search

Search a monotonic yes/no boundary.

Search a Yes/No Boundary

Many problems hide a monotonic predicate: false, false, then true forever. Binary search can find that first true without a sorted array.

# FFFFTTTT  -> find first T

What Monotonic Means

A predicate is monotonic when once it turns true it stays true. That single property is what lets you binary-search the boundary.

def ok(x):
    return x * x >= target

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