Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted Array is LeetCode 153 (Medium). An array of distinct integers was sorted in ascending order and then rotated: each rotation moves the last element to the front, and it was rotated between 1 and n times. Return the smallest value.
- The solution must run in O(log n) time, so scanning for the smallest value is not allowed.
- Rotating n times gives back the sorted array, so the input may not look rotated at all.
- n == nums.length
- 1 <= n <= 5000
- -5000 <= nums[i] <= 5000
- All the integers of nums are unique.
- nums is sorted and rotated between 1 and n times.
Intuition
A rotation cuts the sorted array in two and swaps the pieces. What you get is a run of large values followed by a run of small values, and the minimum is the first value of the small run.
The right end of the search range always lies in the small run. So one comparison places the middle:
- Middle bigger than the right end: the middle is in the large run, and the minimum is somewhere to its right.
- Middle smaller than the right end: the middle is in the small run, which only climbs from the minimum, so the minimum is the middle or to its left.
Either way half the range goes, which gives O(log n).
A sorted array that has been rotated, with a question about its smallest value, its rotation point or how many times it was rotated: binary search on which side of the drop the middle sits.
Approach
Before reading on, take one rotated array and mark which values are bigger than the last element and which are not. Where does the minimum sit in that picture? Then turn it into a binary search in O(log n).
Start with the whole array
Set lo = 0 and hi = n - 1. The last element always belongs to the small run (in an unrotated array the whole array is that run), so the range starts with its right end in the run that holds the minimum.
Middle bigger than the right end: go right
If nums[mid] > nums[hi], the middle is in the large run, and every value from lo to mid is larger than the minimum. Set lo = mid + 1 to drop all of them, including mid.
Middle smaller than the right end: keep it
Otherwise mid is in the small run, which rises from the minimum, so nothing right of mid can be smaller. Set hi = mid, keeping mid because it may be the minimum. The right end stays inside the small run.
Stop when one index is left
Loop while lo < hi. Each step keeps the minimum inside the range and makes it smaller, so when lo == hi that index holds the answer. An unrotated array sends every step left and finishes at index 0 with no special case.
Code in Python, C++ and Java
lo to 4.hi becomes 5, not 4.hi becomes 4, not 3.lo and hi meet at index 4. Every step kept the minimum inside the range, so the one value left, 0, is it. That took 3 comparisons for 7 values.lo to 3.hi becomes 3, not 2.lo and hi meet at index 3. Every step kept the minimum inside the range, so the one value left, 1, is it. That took 2 comparisons for 5 values.hi becomes 1, not 0.hi becomes 0, not -1.lo and hi meet at index 0. Every step kept the minimum inside the range, so the one value left, 11, is it. That took 2 comparisons for 4 values.Common pitfalls
Comparing the middle with the left end
if nums[mid] > nums[lo]:
lo = mid + 1
else:
hi = midif nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = midA middle bigger than the left end happens both when the array is unrotated and when the middle is in the large run, so the test cannot tell which way to go. On [1, 2, 3, 4, 5] it walks right and returns 4.
Dropping mid when moving left
else:
hi = mid - 1else:
hi = midWhen the middle is in the small run it can be the minimum itself. On [4, 5, 1, 2, 3] the first middle is the 1; skipping it leaves [4, 5], and the search returns 4.
Looping while lo <= hi
while lo <= hi:
while lo < hi:
With hi = mid the range never becomes empty. Once lo == hi, the middle is that same index, hi = mid changes nothing, and the loop never ends. A single-element array like [1] hangs at once.
Edge cases
[2, 3, 4, 5, 1]Every middle is bigger than the 1 at the right end, so lo keeps moving right until it reaches index 4. The right end was the answer all along and was never dropped.
Complexity
Find Minimum in Rotated Sorted Array FAQ
How do I find how many times the array was rotated?
Return lo instead of nums[lo]. The index of the minimum is the rotation count, with 0 meaning it was rotated a full n times (or not at all).
Can I check which half is sorted and keep a running minimum instead?
Yes. If nums[lo] <= nums[mid], the left half is sorted, so record nums[lo] and search the right half; otherwise record nums[mid] and search the left. It is also O(log n), but needs the extra running minimum that the right-end comparison does without.
What changes if the array can hold duplicates?
The middle can equal the right end, and then neither half can be ruled out. That version is Find Minimum in Rotated Sorted Array II, whose worst case is O(n). To find a given value rather than the minimum, see Search in Rotated Sorted Array.