Find Minimum in Rotated Sorted Array (LeetCode 153): Solution and Explanation
A sorted array of unique integers has been rotated some number of times, so its smallest element is no longer necessarily at the front. Return that smallest element in logarithmic time.
Watch it run step by step
Try your own input and write the code →Intuition
The obvious answer is to scan every element and keep the smallest, which takes O(n) time. That works, but it ignores the fact that the array is made of two sorted runs, and the problem asks for O(log n).
Picture the rotated array as two ascending runs where every value in the left run is larger than every value in the right run. The minimum is the first element of the right run, the spot where the values drop. If you look at a midpoint and its value is at least nums[l], the midpoint is still inside the left run, so the drop must be further right. If the midpoint value is smaller than nums[l], the midpoint is in the right run, and the minimum is at the midpoint or to its left.
There is also a shortcut that ends the search early. If nums[l] < nums[r], the current range has no rotation inside it at all, so its smallest value is simply nums[l]. The reference solution records candidates in res as it goes, checking each midpoint, so even when the search steps past the minimum, the minimum has already been captured.
Approach: Binary search on the rotation point
- Initialize
res = nums[0]and setl = 0,r = len(nums) - 1. - While
l <= r, first check whethernums[l] < nums[r]. If so, the range is already sorted, so updatereswithnums[l]and stop. - Otherwise compute
m = (l + r) // 2and updateres = min(res, nums[m]). - If
nums[m] >= nums[l], the midpoint is in the left, larger run, so search right withl = m + 1. - Otherwise the midpoint is in the right, smaller run, so search left with
r = m - 1. - Return
reswhen the loop ends.
Step-by-step example
nums = [3,4,5,1,2]
- Start with
res = 3, l = 0, r = 4.nums[0] = 3is not less thannums[4] = 2, so the range is rotated. - m = 2,
nums[2] = 5.resstays 3. Since 5 >=nums[l] = 3, the midpoint is in the left run, so l = 3. - Now l = 3, r = 4.
nums[3] = 1 < nums[4] = 2, so this range is sorted. Updateres = min(3, 1) = 1and break. - Return 1.
Code: Python, Java, C++
1class Solution:
2 def findMin(self, nums: List[int]) -> int:
3 res = nums[0]
4 l, r = 0, len(nums) - 1
5
6 while l <= r:
7 if nums[l] < nums[r]:
8 res = min(res, nums[l])
9 break
10
11 m = (l + r) // 2
12 res = min(res, nums[m])
13 if nums[m] >= nums[l]:
14 l = m + 1
15 else:
16 r = m - 1
17 return res1class Solution {
2 public int findMin(int[] nums) {
3 int res = nums[0];
4 int l = 0, r = nums.length - 1;
5
6 while (l <= r) {
7 if (nums[l] < nums[r]) {
8 res = Math.min(res, nums[l]);
9 break;
10 }
11
12 int m = l + (r - l) / 2;
13 res = Math.min(res, nums[m]);
14
15 if (nums[m] >= nums[l]) {
16 l = m + 1;
17 } else {
18 r = m - 1;
19 }
20 }
21 return res;
22 }
23}1class Solution {
2public:
3 int findMin(vector<int>& nums) {
4 int res = nums[0];
5 int l = 0, r = nums.size() - 1;
6
7 while (l <= r) {
8 if (nums[l] < nums[r]) {
9 res = min(res, nums[l]);
10 break;
11 }
12
13 int m = l + (r - l) / 2;
14 res = min(res, nums[m]);
15
16 if (nums[m] >= nums[l]) {
17 l = m + 1;
18 } else {
19 r = m - 1;
20 }
21 }
22 return res;
23 }
24};Time and space complexity
O(log n)
Each iteration halves the search range, and the loop may exit even earlier when it hits a sorted range, so there are at most about log2(n) iterations.
O(1)
The solution keeps a few integer variables and does not allocate anything that grows with n.
Other approaches
Compare the midpoint with the right end (time O(log n), space O(1))
Loop while l < r; if nums[m] > nums[r] the minimum is to the right so set l = m + 1, otherwise set r = m. When the loop ends, nums[l] is the minimum. It avoids the res variable and is a popular compact form.
Linear scan (time O(n), space O(1))
Return the minimum of the whole array, or find the first index where a value is smaller than its predecessor. Simple, but it does not meet the logarithmic requirement.
Common mistakes
- Forgetting the early exit check
nums[l] < nums[r]. Without it, an unrotated range can pushlpast the minimum and the answer depends entirely onreshaving seen it. - Using
nums[m] > nums[l]instead of>=. Whenm == lin a two-element range, the strict check sends the search the wrong way. - Returning
nums[l]at the end with thisl <= rloop. In this variant the pointers can cross past the minimum, so the trackedresis the answer. - Applying the same logic to arrays with duplicates, where
nums[m] == nums[r]gives no information and needs a separate rule.
Frequently asked questions
Why compare the middle element with the left or right end?
Comparing with an endpoint tells you which sorted run the midpoint belongs to. In the larger left run the minimum lies to the right; in the smaller right run it lies at the midpoint or to the left. That single comparison is enough to discard half the range.
What if the array is not rotated at all?
Then nums[0] < nums[n - 1] on the first iteration, the early exit fires, and the answer is nums[0]. A rotation by n positions produces the original sorted array, so this case is common.
How does this differ from the version with duplicates?
With duplicates you can hit nums[m] == nums[r], which does not reveal which side holds the minimum. The standard fix is to shrink r by one in that case, which keeps the answer correct but makes the worst case O(n).