Find Minimum in Rotated Sorted Array
Medium ArrayBinary Search
Key Insight:In a rotated sorted array, comparing the middle element to the rightmost element reveals which half contains the rotation point—if mid > right, the minimum must be in the right half; otherwise, it's in the left half (including mid).
Approaches
Insight Challenge
If nums[mid] > nums[right], where must the minimum be?
Narrow down candidates by eliminating impossible regions
A rotated sorted array has a special property: it consists of two sorted subarrays. The minimum element is at the boundary between them (the rotation point). By comparing the middle element with a reference point, we can determine which half contains the rotation point and eliminate the other half. This allows us to reduce the search space by half each iteration.
Algorithm
- Define the search boundaries at array start and end
- While multiple candidates remain:
- Examine the middle element
- If middle is greater than rightmost, the break point is in the right half
- Otherwise, the break point is in the left half (including middle)
- Return the element at the converged position
Solution
Time O(log n) Space O(1)
def find_min(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]