Week 2 · Day 11 of 42
Binary Search: On Sorted Array/List and Find First/Last Occurrence
Sorted input + find target/position → classic binary search. Use lo <= hi when you need to check every element; use lo < hi when you converge to a single answer. Need leftmost or rightmost position of a value → use bisect_left / bisect_right logic. For first: when nums[mid] >= target, go left (hi = mid). For last: when nums[mid] <= target, go right (lo = mid + 1), then subtract 1.
Checking access
Spotted a mistake on this page?
Wrong formula, broken animation or confusing wording. Tell us and we will fix it.