Find Minimum in Rotated Sorted Array
Master this topic with zero to advance depth.
Find Minimum in Rotated Sorted Array
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. Given the sorted rotated array nums of unique elements, return the minimum element of this array. You must write an algorithm that runs in time.
Examples
The original array was [1,2,3,4,5] rotated 3 times.
The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
Level I: Brute Force (Linear Scan)
Intuition
Simply scan the array and find the minimum element. We don't exploit the sorted/rotated property.
Detailed Dry Run
nums = [3,4,5,1,2]. Min starts at 3. Compare with 4, 5, 1 (new min), 2. Result: 1.
Level III: Optimal (Binary Search)
Intuition
In a rotated sorted array, if nums[mid] > nums[right], the minimum must be in the right half (excluding mid). Otherwise, it's in the left half (including mid).
Detailed Dry Run
| Step | L | R | Mid | nums[Mid] | nums[R] | Decision |
|---|---|---|---|---|---|---|
| 1 | 0 | 4 | 2 | 5 | 2 | 5 > 2 → L = 3 |
| 2 | 3 | 4 | 3 | 1 | 2 | 1 < 2 → R = 3 |
| Exit | 3 | 3 | - | - | - | Return nums[3] = 1 |
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.