EasyVery High Frequency25 min
Binary Search Fundamentals
Left/right boundary templates — the correct way to write bug-free binary search.
Binary search halves the search space each step. Requires sorted data or monotonic condition. Three templates: exact match, left-bound, right-bound. O(log n).
Binary Search
Operations — Step by Step
Binary Search (Exact Match)
O(log n)Halves search space each step. 1 million elements → at most 20 stepsStep-by-step1 / 4
lo=0, hi=6, target=5
Full array. mid = 0+(6-0)/2 = 3. arr[3]=7.