Find Minimum in Rotated Sorted Array (LeetCode 153): Solution and Explanation

Medium
Array
Binary Search

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.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

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

  1. Initialize res = nums[0] and set l = 0, r = len(nums) - 1.
  2. While l <= r, first check whether nums[l] < nums[r]. If so, the range is already sorted, so update res with nums[l] and stop.
  3. Otherwise compute m = (l + r) // 2 and update res = min(res, nums[m]).
  4. If nums[m] >= nums[l], the midpoint is in the left, larger run, so search right with l = m + 1.
  5. Otherwise the midpoint is in the right, smaller run, so search left with r = m - 1.
  6. Return res when the loop ends.

Step-by-step example

nums = [3,4,5,1,2]
  1. Start with res = 3, l = 0, r = 4. nums[0] = 3 is not less than nums[4] = 2, so the range is rotated.
  2. m = 2, nums[2] = 5. res stays 3. Since 5 >= nums[l] = 3, the midpoint is in the left run, so l = 3.
  3. Now l = 3, r = 4. nums[3] = 1 < nums[4] = 2, so this range is sorted. Update res = min(3, 1) = 1 and break.
  4. 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 res

Time and space complexity

Time

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.

Space

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 push l past the minimum and the answer depends entirely on res having seen it.
  • Using nums[m] > nums[l] instead of >=. When m == l in a two-element range, the strict check sends the search the wrong way.
  • Returning nums[l] at the end with this l <= r loop. In this variant the pointers can cross past the minimum, so the tracked res is 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).