Binary search
easy 16 minBinary search finds a value, a boundary or an insertion point in sorted data by checking the middle and discarding the half that cannot hold the answer.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the time complexity of binary search on a sorted array of n elements?
Why use `left + (right - left) // 2` instead of `(left + right) // 2`?
To find the first occurrence of a target, what should you do when arr[mid] equals the target?
In a lower bound search for the first arr[i] >= target, why set right = mid rather than right = mid - 1 when arr[mid] >= target?
A closed-interval search (`while left <= right`) runs on [2, 5, 8, 12, 19, 24] for target 10. What are left and right when the loop ends?
What is the maximum number of comparisons binary search needs on a sorted array of 1,000 elements?
On a = [1, 2, 4, 4, 4, 6, 8], what do bisect_left(a, 4) and bisect_right(a, 4) return?
A half-open search uses `left, right = 0, len(arr)` but the loop is `while left <= right`. What happens when the target is bigger than every value?
Which loop can run forever?
Packages [2, 4, 1, 3] must ship in order within 2 days. Binary search on capacity starts with lo and hi equal to what?
Pick an answer to see why.
Not quite. Each comparison halves the remaining range, so after about log₂ n halvings one candidate is left: O(log n).
Not quite. Both give the same value, but left + right can exceed the largest 32-bit integer in languages like Java and C++. right - left always fits.
Not quite. There may be an earlier copy. Save mid as the best answer so far and set right = mid - 1 to keep searching the left side.
Not quite. mid is a valid candidate. Setting right = mid - 1 would discard it and could skip the very first index that qualifies.
Not quite. mid = 2 (8): left = 3. mid = 4 (19): right = 3. mid = 3 (12): right = 2. Now left = 3 > right = 2. Left is also the insertion point of 10.
Not quite. floor(log₂ 1000) + 1 = 9 + 1 = 10, since 2⁹ = 512 ≤ 1000 < 1024 = 2¹⁰.
Not quite. The first value ≥ 4 is at index 2 and the first value > 4 is the 6 at index 5. Their difference, 3, is the number of 4s.
Not quite. left climbs to len(arr) while right is still len(arr), so the loop runs once more with mid = len(arr), which is out of range. Half-open ranges need while left < right.
Not quite. When right = left + 1 the floor midpoint equals left, so left = mid changes nothing. Rounding mid up, as in the last option, fixes it.
Not quite. Capacity below the heaviest package (4) can never ship it, and the total weight (10) always fits in one day. The answer, 6, lies between them.
Flashcards
Say your answer, then reveal it.
Card 1 of 12
What does binary search do on each step?
Looks at the middle of the live range and discards the half that cannot contain the target, by moving left or right past mid.
Say your answer, then reveal it.
What must be true before you can use binary search?
The data is sorted, or more generally the test is monotonic (no, no, ..., yes, yes), and the structure has random access.
Say your answer, then reveal it.
Worst-case number of comparisons for n items?
floor(log2 n) + 1. That is 5 for 16 items, 10 for 1,000, 20 for 1,000,000 and 30 for a billion.
Say your answer, then reveal it.
Why left + (right - left) // 2 instead of (left + right) // 2?
Same value, but left + right can overflow a 32-bit integer in Java or C++. right - left always fits.
Say your answer, then reveal it.
When does the loop use <= and when < ?
Closed interval [left, right] with right = len - 1: while left <= right. Half-open [left, right) with right = len: while left < right.
Say your answer, then reveal it.
What is LIMP?
Loop while left <= right, Index: compute mid safely, Match: compare arr[mid] with the target, Pointer: move left or right past mid.
Say your answer, then reveal it.
How does first occurrence differ from classic search?
On a match it saves mid and sets right = mid - 1 to keep looking left, then returns the saved result. Last occurrence sets left = mid + 1 instead.
Say your answer, then reveal it.
Lower bound versus upper bound?
Lower bound is the first index with arr[i] >= x (bisect_left). Upper bound is the first index with arr[i] > x (bisect_right). Count of x = upper - lower.
Say your answer, then reveal it.
In lower bound, why right = mid and not mid - 1?
When arr[mid] >= target, mid itself may be the first valid index. right = mid - 1 would throw it away.
Say your answer, then reveal it.
Where does left end up when classic search misses?
At the insertion point: every value before it is smaller than the target and every value from it on is bigger.
Say your answer, then reveal it.
What is binary search on the answer?
Binary searching a range of possible answers with a monotonic feasibility check ok(x), looking for the first x that works. Cost O(log R) checks.
Say your answer, then reveal it.
What causes an infinite loop in binary search?
An update that does not shrink the range, usually left = mid with a floor midpoint when right = left + 1. Round mid up or move past it.
Say your answer, then reveal it.

These cards come back in Today, just before you would forget them.