Classic form
Keep lo..hi inclusive. Compare mid to target. Discard the half that cannot contain the answer. Exit when interval empties.
Lesson
Halve a monotonic search space each step — O(log n) probes.
Loading solution…
Keep lo..hi inclusive. Compare mid to target. Discard the half that cannot contain the answer. Exit when interval empties.
When the array isn't sorted but a feasibility function is monotonic (can finish in X days?), binary search on X.
Binary Search studio shows lo/hi/mid and discarded halves lighting up each probe.
Showing 24 of the related problems in the 1000-question bank.
Median of Two Sorted Arrays
Binary Search
Search in Rotated Sorted Array
Binary Search
Find First and Last Position of Element in Sorted Array
Binary Search
Search Insert Position
Binary Search
Search a 2D Matrix
Binary Search
Find Minimum in Rotated Sorted Array
Binary Search
Two Sum II - Input Array Is Sorted
Two Pointers
Search a 2D Matrix II
Two Pointers / BS
Find the Duplicate Number
Floyd
Longest Increasing Subsequence
DP / Patience
Binary Search
Binary Search
Koko Eating Bananas
Binary Search on Answer
Time Based Key-Value Store
Design
First Bad Version — Practice 17
Binary Search
Minimum Number of Days to Make m Bouquets — Practice 18
Binary Search
Split Array Largest Sum — Practice 20
Binary Search
Sqrt(x) — Practice 22
Binary Search
Guess Number Higher or Lower — Practice 24
Binary Search
Time Needed to Buy Tickets (BS) — Practice 25
Binary Search
Capacity To Ship Packages — Practice 27
Binary Search
Valid Perfect Square — Practice 29
Binary Search
Magnetic Force Between Balls — Practice 30
Binary Search
Find K Closest Elements — Practice 32
Binary Search
Find Peak Element — Practice 34
Binary Search
Got the lesson?
Mark Binary Search as understood when the ideas stick.