Thirteen problems that drill the binary-search pattern from Lesson 10 — the plain “find an element” variety, the rotated-array twists, and the powerful “binary search on the answer” family. Open a problem, try it yourself first, then read how to reach the solution. Each solution here was actually run and tested.
How to practise. The bugs in binary search are always the boundaries. Before you code, fix two things in your head: the invariant (is the answer always inside
[lo, hi]?) and the exit condition (lo <= hiwhen you return from inside the loop;lo < hiwhen the answer is wherever the pointers meet). For the “search on the answer” ones, first write theok(x)feasibility check, then binary-search the smallestxthat passes.
Three of the solutions below use the standard library — add these imports at the top:
import bisect
import math
from collections import defaultdictThe 13 problems
- 1Binary Search (LeetCode 704)EasyFind a target's index in a sorted array: halve the search range each step by comparing with the middle.arrow_forward
- 2Search Insert Position (LeetCode 35)EasyReturn the target's index, or where it would be inserted: find the first element >= target, i.e. bisect_left.arrow_forward
- 3First Bad Version (LeetCode 278)EasyFind the first bad version with few API calls: binary-search the boundary where good turns to bad.arrow_forward
- 4Search in Rotated Sorted Array (LeetCode 33)MediumSearch a rotated sorted array in O(log n): one half around mid is always sorted, so test the target there.arrow_forward
- 5Find Minimum in Rotated Sorted Array (LeetCode 153)MediumFind the minimum of a rotated sorted array: compare mid with the right end to see which side holds the drop.arrow_forward
- 6Find First and Last Position of Element in Sorted Array (LeetCode 34)MediumFind the first and last index of a target in a sorted array with two boundary searches.arrow_forward
- 7Search a 2D Matrix (LeetCode 74)MediumSearch a row-major sorted matrix: treat it as one sorted array of R*C cells and map indices with divmod.arrow_forward
- 8Find Peak Element (LeetCode 162)MediumFind any peak in an unsorted array in O(log n): compare mid with its right neighbour and always walk uphill.arrow_forward
- 9Koko Eating Bananas (LeetCode 875)MediumFind the slowest eating speed that finishes in h hours: binary-search the speed, checking feasibility.arrow_forward
- 10Capacity To Ship Packages Within D Days (LeetCode 1011)MediumFind the least ship capacity that delivers packages in order within D days: binary-search it, pack greedily.arrow_forward
- 11Time Based Key-Value Store (LeetCode 981)MediumStore timestamped values per key; get(key, t) binary-searches for the latest timestamp at or before t.arrow_forward
- 12Split Array Largest Sum (LeetCode 410)HardSplit an array into k parts minimising the largest sum: binary-search the cap and count pieces greedily.arrow_forward
- 13Median of Two Sorted Arrays (LeetCode 4)HardFind the median of two sorted arrays in O(log min(m, n)): binary-search a partition of the smaller array.arrow_forward