Find the smallest value in a sorted array that was rotated once.
Start with the concrete trace below. It shows the state the algorithm must carry as it runs.
Problem trace
Find Minimum in Rotated Sorted Array: Retain the minimum by comparing middle with the current right boundary.
- Recognize it
- Distinct values form two increasing runs after one rotation, and the task asks for the smallest value rather than a particular target.
- Keep true
- The minimum is always inside inclusive [left, right]. If nums[middle] exceeds nums[right], middle is on the high run; otherwise middle may be the minimum and must be retained.
- Reuse it
- Pivot searches depend on choosing an endpoint reference and preserving the candidate when equality or ordering does not prove it impossible; this same boundary discipline applies to first-occurrence searches.
Pattern: Binary search against the right value.
Simple idea: If the middle value is larger than the right value, the minimum must be to the right of the middle. Otherwise, the middle may be the minimum, so keep it.
def find_min_rotated(nums: list[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
middle = (left + right) // 2
if nums[middle] > nums[right]:
left = middle + 1
else:
right = middle
return nums[left]
Cost: time and space.