Modified Binary Search Cheat Sheet

You cut a sorted range in half each step instead of scanning it. lo and hi close in until they cross and land on the left-most or right-most match.

Modified Binary Search cheat sheet preview

Earn the full sheet below.

When to use it

  • The list is sorted (or reframed as a sorted range of answers) and a plain scan is too slow.
  • You need the left-most or right-most position of a target, not just any match.
  • The question asks for O(log n) on a list, or it runs a checker function across a range of answers.
  • The list is 'almost sorted' (rotated), but binary search still works once you find the clean half.

What's on the sheet

The halving is identical every time. Step 3 decides what found means.

  1. Variable Initialization. lo and hi bracket the range; result holds the hit.
  2. Binary Search Initialization. The loop and mid never change shape.
  3. Validation. When you match, record it and keep hunting one side.
  4. Search. Always skip past mid. Reusing it as a bound loops forever.

Watch out

  • The loop condition is lo <= hi, not just <. Drop the equals and you'll skip the last remaining candidate.
  • Finding the target isn't the finish line for left-most or right-most questions like First and Last Positions in a Sorted List. Keep narrowing instead of breaking.

How to earn it

Solve half of the Modified Binary Search problems and the sheet is yours. A free account works fine, and everyone earns their sheets the same way.

Want to see a finished sheet first? Preview the Two Pointers sample.

Every pattern has a sheet. See the full set.