Find Minimum in Rotated Sorted Array
Medium
Topics
A sorted array of unique integers was rotated at some pivot. Given the array nums, return the minimum element in O(log n).
Example 1
minimumthe rotation break
A rotated sorted array is two ascending runs. The minimum is wherever the drop is, index 3.
Cells are the array values; the small numbers underneath are indices.
Input: nums = [3,4,5,1,2] Output: 1
Example 2
minimumthe rotation break
Input: nums = [4,5,6,7,0,1,2] Output: 0
Constraints
- 1 <= nums.length <= 5000
- All integers are unique.
- The array is a rotation of an ascending sort.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Compare the middle element to the rightmost. If the middle is bigger, the rotation point (and thus the minimum) lies to its right; otherwise it is at the middle or to its left.
Approach
- Binary search with lo, hi.
- If
nums[mid] > nums[hi], the min is in(mid, hi], so lo = mid + 1. - Else hi = mid. Converge to the minimum.
Dry Run
nums = [4, 5, 6, 7, 0, 1, 2]
The invariant is that the minimum always lies in [lo, hi], and the comparison is
against the RIGHT end.
lo=0 hi=6 mid=3 nums[3]=7 > nums[6]=2 the pivot is to the right lo=4
lo=4 hi=6 mid=5 nums[5]=1 <= nums[6]=2 mid could BE the min hi=5
lo=4 hi=5 mid=4 nums[4]=0 <= nums[5]=1 mid could BE the min hi=4
lo == hi == 4
answer nums[4] = 0
Comparing against nums[hi] rather than nums[lo] is what makes a non-rotated array
work without a special case. If nums[mid] is at most nums[hi], the right side is
sorted and the minimum is at mid or earlier; otherwise the rotation point must be
past mid. Compare against nums[lo] instead and all three of these go wrong:
[4,5,6,7,0,1,2] returns 2, [1,2,3,4,5] returns 4, and even [2,1] returns 2.
Note hi = mid, not mid - 1. mid is still a candidate, so discarding it could throw
away the answer; lo = mid + 1 is safe because a strictly greater value cannot be
the minimum.
The loop is `while lo < hi`, not `<=`. It converges on a single index rather than
searching for a value, so there is no "not found" case to handle.Complexity
- Time — O(log n): the search space halves each step.
- Space — O(1): two pointers.
Common Pitfalls
- Compare against the right end, not the left, so a non-rotated array still works.
mid = (lo + hi) / 2is the textbook form, and in Java or C++ it is the one that bites: the momentlo + hipasses2^31 - 1it wraps negative and the index flies out of range. This problem’s bounds never get close, so the code above is correct as written, butlo + (hi - lo) / 2is the habit worth keeping — same answer, same cost, no overflow. Python’s integers are arbitrary precision, so it is a non-issue there.
Full Solution — all languages
class Solution:
def findMin(self, 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]import java.util.*;
class Solution {
public int findMin(int[] nums) {
int lo=0,hi=nums.length-1;
while(lo<hi) {
int mid=(lo+hi)/2;
if(nums[mid]>nums[hi])lo=mid+1;
else hi=mid;
}
return nums[lo];
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findMin(vector<int>& nums) {
int lo=0,hi=nums.size()-1;
while(lo<hi) {
int mid=(lo+hi)/2;
if(nums[mid]>nums[hi])lo=mid+1;
else hi=mid;
}
return nums[lo];
}
};/**
* @param {number[]} nums
* @return {number}
*/ var findMin = function(nums) {
let lo=0,hi=nums.length-1;
while(lo<hi) {
const mid=(lo+hi)>>1;
if(nums[mid]>nums[hi])lo=mid+1;
else hi=mid;
}
return nums[lo];
};