Binary Search — Complete Guide: All Patterns & Problem Index
Advertisement
Binary Search — Complete Pattern Guide
Problems 301–345 · 45 posts · Easy × 8, Medium × 27, Hard × 10
Core Patterns
| # | Pattern | Condition | Example |
|---|---|---|---|
| 1 | Classic — Exact Target | nums[mid] == target | Binary Search, Search in Matrix |
| 2 | Left Boundary (first true) | lo = mid+1 when condition false | First Bad Version, Find First/Last |
| 3 | Right Boundary (last true) | hi = mid-1 when condition true | Floor/Ceiling, Find Closest |
| 4 | Rotated Array | Check which half is sorted | Search Rotated Array |
| 5 | BS on Answer | Binary search on result space | Koko Eating Bananas, Capacity Ships |
| 6 | 2D Matrix | Treat as 1D array | Search a 2D Matrix |
| 7 | Peak Finding | Eliminate downhill halves | Find Peak Element |
The Universal Template
# Left boundary (first index where condition is True)
def binary_search(lo, hi):
while lo < hi:
mid = lo + (hi - lo) // 2
if condition(mid):
hi = mid # answer could be mid
else:
lo = mid + 1 # answer is after mid
return lo # lo == hi == first True position
# Right boundary (last index where condition is True)
def binary_search_right(lo, hi):
while lo < hi:
mid = lo + (hi - lo + 1) // 2 # upper mid to avoid infinite loop
if condition(mid):
lo = mid # answer could be mid
else:
hi = mid - 1 # answer is before mid
return lo
Avoid Integer Overflow
Always use mid = lo + (hi - lo) // 2 instead of (lo + hi) // 2.
BS on Answer Template
def bs_on_answer(lo, hi):
# lo = min possible answer, hi = max possible answer
while lo < hi:
mid = (lo + hi) // 2
if feasible(mid): # can we achieve 'mid'?
hi = mid # try smaller (minimize)
else:
lo = mid + 1 # need larger
return lo
Problem Index — Binary Search
Easy (301–308)
| # | Problem | Pattern |
|---|---|---|
| 301 | Binary Search | Classic Exact |
| 302 | First Bad Version | Left Boundary |
| 303 | Search Insert Position | Left Boundary |
| 304 | Guess Number Higher or Lower | Classic |
| 305 | Sqrt(x) | Right Boundary |
| 306 | Count Negative Numbers in Sorted Matrix | Row Binary Search |
| 307 | Check If N and Its Double Exist | Classic |
| 308 | Valid Perfect Square | Classic / Math |
Medium (309–335)
| # | Problem | Pattern |
|---|---|---|
| 309 | Find First and Last Position | Left+Right Boundary |
| 310 | Search in Rotated Sorted Array | Rotated |
| 311 | Find Minimum in Rotated Sorted Array | Rotated (min) |
| 312 | Search a 2D Matrix | 2D as 1D |
| 313 | Search a 2D Matrix II | Staircase Search |
| 314 | Koko Eating Bananas | BS on Answer |
| 315 | Minimum Number of Days to Make Bouquets | BS on Answer |
| 316 | Find Smallest Letter Greater Than Target | Circular BS |
| 317 | Capacity to Ship Packages | BS on Answer |
| 318 | Split Array Largest Sum | BS on Answer |
| 319 | Find Peak Element | Peak BS |
| 320 | Find K Closest Elements | BS + Expand |
| 321 | Random Pick with Weight | Prefix Sum + BS |
| 322 | Magnetic Force Between Two Balls | BS on Answer |
| 323 | Missing Element in Sorted Array | BS on Gap |
| 324 | Time Based Key-Value Store | BS on sorted times |
| 325 | Successful Pairs of Spells and Potions | Sort + BS |
| 326 | Maximum Value at a Given Index | BS on Answer |
| 327 | Single Element in Sorted Array | Parity BS |
| 328 | Find the Duplicate Number (BS) | BS on count |
| 329 | Longest Increasing Subsequence (Patience) | BS patience sort |
| 330 | H-Index II | Left Boundary |
| 331 | Online Election | BS on sorted times |
| 332 | Search in Rotated Sorted Array II | Rotated with dups |
| 333 | Count of Range Sum | BS / Merge Sort |
| 334 | Minimize Maximum of Array | BS on Answer |
| 335 | Kth Smallest in Sorted Matrix | BS on Value |
Hard (336–345)
| # | Problem | Pattern |
|---|---|---|
| 336 | Find Minimum in Rotated Array II | Rotated with dups |
| 337 | Median of Two Sorted Arrays | BS on partition |
| 338 | Count Smaller Numbers After Self | Merge Sort / BIT |
| 339 | Minimum Number of Days to Disconnect | BS + Connectivity |
| 340 | Russian Doll Envelopes | 2D LIS + BS |
| 341 | Split Array Largest Sum (Hard) | BS on Answer |
| 342 | Aggressive Cows | BS on Answer |
| 343 | K-th Smallest Prime Fraction | BS on Value |
| 344 | Cutting Ribbons | BS on Answer |
| 345 | Binary Search Master Recap | Cheatsheet |
Advertisement