Find Minimum in Rotated Sorted Array takes a sorted array that someone "spun" around a hidden point and asks you to find the smallest value — in O(log n). It is a sharp lesson in modified binary search: you are not searching for a value, you are searching for the rotation point.
Problem. A sorted array of distinct integers has been rotated between 1 and n times (the front
chunk was moved to the back). Return the minimum element. You must do it in O(log n).
Example: nums = [3, 4, 5, 1, 2] → answer 1. The original [1, 2, 3, 4, 5] was rotated so [1, 2]
moved to the end; the spot where 1 sits is the rotation point.
The slow way first
The obvious answer is to scan every element and track the smallest — that is O(n). It works, but it throws away the structure we were given. The array is almost sorted; binary search should still apply. The challenge says O(log n), so a linear scan is not the intended solution.
The idea: search for the rotation point
The minimum is the only element smaller than the one before it — the seam where the array wraps around. To locate that seam with binary search, look at the middle element and ask one question: is nums[mid] bigger than nums[hi]?
- If
nums[mid] > nums[hi], the right half is "out of order," so the seam (the min) must be to the right ofmid. Movelo = mid + 1. - Otherwise
nums[mid] <= nums[hi], meaning the right half frommidonward is sorted, so the min ismiditself or to its left. Movehi = mid(keepmid— it could be the answer).
Compare against nums[hi], not nums[lo]. Comparing to the right end is what makes the two cases clean: hi = mid (not mid - 1) because mid might be the minimum.
Walk through it
Step through the animation with nums = [3, 4, 5, 1, 2]. lo and hi bracket the array. First mid = 2 (value 5); since 5 > 2 the min is to the right, so lo jumps to 3. Next mid = 3 (value 1); since 1 <= 2 the min is at mid or left, so hi = mid = 3. Now lo == hi, the loop stops, and nums[lo] = 1 is the answer.
Pseudocode
lo, hi = 0, last index
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]: # right half is rotated
lo = mid + 1 # min is strictly to the right
else: # right half (from mid) is sorted
hi = mid # min is mid or to its left -- keep mid
return nums[lo] # lo == hi points at the minimumThe Python solution
def find_min(nums):
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = mid
return nums[lo]- The loop condition is
lo < hi(not<=): we shrink the window until a single element remains. - We compare
nums[mid]tonums[hi]— the right end — which cleanly tells us which half holds the seam. lo = mid + 1discardsmidbecause, being larger thannums[hi], it cannot be the minimum.hi = midkeepsmidbecause it might be the minimum itself.- When the loop ends,
lo == hiandnums[lo]is the answer — no separate "found" check needed.
Complexity
| Case | Time | Notes |
|---|---|---|
| Linear scan | O(n) (moderate) | ignores the sorted structure |
| Binary search (this solution) | O(log n) (fast) | halve the window each step |
O(1) (fast)Each iteration throws away half the remaining elements, so we finish in about log n steps using only two index variables.
When this pattern shows up
When an array is sorted but "broken" at one point — rotated arrays, search in rotated sorted array,
finding a peak — reach for binary search on a condition rather than on a target value. Pick one
comparison (here, nums[mid] vs nums[hi]) that reliably tells you which half to keep.
Use hi = mid, not hi = mid - 1, in the else branch. The middle element can be the minimum, so
cutting it off can skip the answer. Likewise compare to nums[hi], not nums[lo]: comparing to lo
needs an extra "already sorted" check and is a classic source of bugs.
Practice
For nums = [4, 5, 6, 7, 0, 1, 2], the first mid is index 3 (value 7) and nums[hi] = 2. Which way does the search go?
1. Why do we compare nums[mid] with nums[hi] instead of nums[lo]?
2. Why is the else branch hi = mid and not hi = mid - 1?
3. What is the value of lo when the loop ends?
4. What is the time complexity of this solution?