Skip to content

Find Minimum in Rotated Sorted Array

01 · Question

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. Given the rotated array nums of unique elements, return the minimum element.

Example:

  • Input: nums = [4,5,6,7,0,1,2]
  • Output: 0

02 · Solution

Reference solution

1def findMin(nums: List[int]) -> int:
2 l, r = 0, len(nums) - 1
3
4 while l < r:
5 mid = (l + r) // 2
6
7 if nums[mid] > nums[r]:
8 l = mid + 1
9 else:
10 r = mid
11
12 return nums[l]