Find Minimum in Rotated Sorted Array
Compare against right boundary
Binary search on a property, not a value. Which half is sorted?
Problem
Given a rotated sorted array nums with distinct values, return the minimum element.
The array was originally sorted in ascending order, then rotated at an unknown pivot.
Intuition
The rotation creates two sorted runs. One of them contains the minimum; the other does not.
The trick is to compare the midpoint against the right boundary:
- if
nums[mid] > nums[hi], the minimum must be strictly to the right ofmid; - otherwise,
midcould be the minimum, so keep it in play by movinghileft tomid.
This is binary search on a property, not on a value. You are not asking "where is 0?" You are asking "which side of the pivot is this midpoint on?"
Approach
Binary Search on the Pivot
O(log n)O(1)Use a closed interval [lo, hi] with hi initialized to the last index. The minimum is always inside the interval.
- If
nums[mid] > nums[hi], the right half contains the pivot, so setlo = mid + 1. - Otherwise, the minimum is at
midor to its left, so sethi = mid.
When lo == hi, the interval has collapsed to the minimum.
impl Solution {
pub fn find_min(nums: Vec<i32>) -> i32 {
let mut lo = 0usize;
let mut hi = nums.len() - 1;
while lo < hi {
let mid= lo + (hi - lo) / 2;
if nums[mid] > nums[hi] {
lo = mid + 1;
} else {
hi = mid;
}
}
nums[lo]
}
}
function findMin(nums: number[]): number {
let lo = 0;
let hi = nums.length - 1;
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (nums[mid] > nums[hi]) {
lo = mid + 1;
} else {
hi = mid;
}
}
return nums[lo];
}Trace
Find the minimum in nums = [4, 5, 6, 7, 0, 1, 2]:
| lo | hi | mid | nums[mid] | nums[hi] | action |
|---|---|---|---|---|---|
| 0 | 6 | 3 | 7 | 2 | move lo to 4 |
| 4 | 6 | 5 | 1 | 2 | move hi to 5 |
| 4 | 5 | 4 | 0 | 1 | move hi to 4 |
The interval collapses onto index 4, which holds the minimum 0.
Edge cases
- Already sorted array — no rotation means the first element is the minimum.
- One element — the loop never runs; return that element.
- Pivot near the end — the same comparison still isolates the minimum without special cases.
Takeaway
Binary search works on structure, not just values. Once you can prove that one side of the midpoint has a property the other side lacks, the search becomes mechanical.
This exact habit reappears in answer-space binary search and in many rotated or partially ordered data problems later.
Get the template perfect: lo, hi, mid, and the loop condition. Practice until it’s automatic.
Lower bound. Understand why lo ends up at the insertion point.
Treat the 2D matrix as a flattened sorted array. Index arithmetic.
Binary search on the ANSWER. This is the gateway to a whole class of hards.
More in Binary Search Mastery
Get the template perfect: lo, hi, mid, and the loop condition. Practice until it’s automatic.
Lower bound. Understand why lo ends up at the insertion point.
Treat the 2D matrix as a flattened sorted array. Index arithmetic.
Binary search on the ANSWER. This is the gateway to a whole class of hards.