Problem
A sorted array with no duplicates has been rotated at some unknown pivot, so [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]. Given the rotated array, return its minimum element without sorting it.
- Input:
[4, 5, 6, 7, 0, 1, 2] - Output:
0 - Explanation: The original sorted order was rotated starting at index 4, placing
0in the middle of the array.
A non-rotated array like [1, 3, 5] is also valid โ the minimum is just the first element.
Intuition
Rotation splits the array into two sorted halves: a larger-valued left portion and a smaller-valued right portion. The minimum sits exactly at the transition between them. Because this structure exists, binary search can determine which half contains the transition point at every step, cutting the search space in half each time rather than scanning every element.
Approach 1 โ Linear Scan
Scan every element and track the smallest value found. This ignores the sorted-halves structure entirely.
- Initialize
current_minto the first element. - Iterate through remaining elements one by one.
- Update
current_minwhenever a smaller value is encountered. - Return
current_minafter the full scan.
1def findMin(nums: list[int]) -> int:
2 current_min = nums[0]
3 for value in nums[1:]:
4 if value < current_min:
5 current_min = value
6 return current_minTime: O(n) โ every element is visited exactly once.
Space: O(1) โ only a single tracking variable is needed.
Approach 2 โ Binary Search
Eliminate half the search space per step by exploiting the two-sorted-halves structure.
The key observation: if nums[mid] > nums[right], the left half is entirely greater than the right half's smallest element, so the minimum must be somewhere to the right of mid. Otherwise the minimum is at mid or to its left.
- Set
left = 0,right = len(nums) - 1. - While
left < right, computemid = left + (right - left) // 2. - If
nums[mid] > nums[right], the inflection point is right ofmidโ setleft = mid + 1. - Otherwise the inflection point is at
midor left of it โ setright = mid(keepmidin range). - When
left == right, the pointers have converged on the minimum โ returnnums[left].
1def findMin(nums: list[int]) -> int:
2 left, right = 0, len(nums) - 1
3
4 while left < right:
5 mid = left + (right - left) // 2
6
7 if nums[mid] > nums[right]:
8 # Left half is the "large" rotated portion; minimum is to the right
9 left = mid + 1
10 else:
11 # Mid could be the minimum itself, so don't exclude it
12 right = mid
13
14 return nums[left]Time: O(log n) โ the search space halves on every iteration.
Space: O(1) โ only two index pointers are maintained.
Complexity Summary
| Approach | Time | Space | When to use |
|---|---|---|---|
| Linear Scan | O(n) | O(1) | Array is small or unsorted โ no structure to exploit |
| Binary Search | O(log n) | O(1) | Input is guaranteed to be a rotated sorted array with no duplicates |
Common Mistakes
- Comparing
midtoleftinstead ofrightโ comparing against the left boundary doesn't reliably identify which half contains the minimum, because the left element may or may not be in the "large" rotated portion. The right boundary is always in the smaller portion, making it the correct reference point. - Using
right = mid - 1instead ofright = midโ whennums[mid] <= nums[right],miditself might be the minimum. Excluding it withmid - 1can skip the answer entirely. - Initializing
right = len(nums)instead oflen(nums) - 1โ this causes an out-of-bounds access when computingnums[right]inside the loop. - Using
left <= rightas the loop condition โ this results in an infinite loop whenleft == rightbecause neither branch changes both pointers when they're equal. - Integer overflow on
midcalculation โ(left + right) / 2can overflow in Java and C++ with large indices. Always useleft + (right - left) / 2.
Related Problems
Search in Rotated Sorted Arrayโ same rotated structure, but you're locating a specific target value instead of the minimumSearch in Rotated Sorted Array IIโ extends the pattern to handle duplicate elements, requiring extra care at the boundariesFind Peak Elementโ binary search to find a local maximum rather than minimum, using the same "eliminate the downhill half" logicSingle Element in a Sorted Arrayโ binary search to locate an anomaly in a sorted array, same pattern of using structural invariants to prune halvesBinary Searchโ the foundational template this problem builds on