Find Minimum in Rotated Sorted Array
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
An array sorted in ascending order with distinct values was rotated some number of times. Given the rotated array nums, return its minimum element, in O(log n) time.
Understand the problem
- Sorted in ascending order means from smallest to largest:
[0, 1, 2, 4, 5, 6, 7]. - Rotating once takes the last element and moves it to the front. Rotating
[0, 1, 2, 4, 5, 6, 7]four times gives[4, 5, 6, 7, 0, 1, 2]. It's like cutting a deck of cards: the order is kept, but it starts in a different place. - It may have been rotated zero times, in which case it's still fully sorted.
- Distinct means no value appears twice.
- O(log n) means you can't just look at every element. You have to throw away half of the remaining elements at every step. (That's binary search: see the beginner guide linked at the top.)
Example 1
Input: nums = [3, 4, 5, 1, 2]
Output: 1The original was [1, 2, 3, 4, 5], rotated 3 times.
Example 2
Input: nums = [4, 5, 6, 7, 0, 1, 2]
Output: 0Example 3
Input: nums = [11, 13, 15, 17]
Output: 11Not rotated at all, so the minimum is the first element.
Constraints
1 <= nums.length <= 5000-5000 <= nums[i] <= 5000- All values are distinct.
Hints
Stuck? Open one hint at a time. Each one gives away a little more than the last.
The idea in one sentence
Use binary search, comparing the middle element with the rightmost one to decide which half contains the "drop" where the minimum sits.
The naive approach (and why it's not enough)
Loop through the array and keep the smallest value. That's O(n), and it's correct, but the problem asks for O(log n): you're not allowed to look at every element.
The key insight
A rotated sorted array is two sorted runs, one after the other:
[4, 5, 6, 7, 0, 1, 2]
high run low run
(bigger) (smaller)The minimum is the first element of the low run. We can find which run the middle element belongs to with one comparison against the rightmost element (which is always in the low run, or the array isn't rotated at all):
nums[mid] > nums[right]→ the middle is in the high run. The minimum is strictly to the right:left = mid + 1.nums[mid] < nums[right]→ the middle is in the low run. The minimum ismidor to its left:right = mid.
Each step throws away half the range. That's binary search.
Step by step
nums = [4, 5, 6, 7, 0, 1, 2] (indices 0 to 6):
left | right | mid | nums[mid] | nums[right] | Compare | Move |
|---|---|---|---|---|---|---|
| 0 | 6 | 3 | 7 | 2 | 7 > 2 | left = 4 |
| 4 | 6 | 5 | 1 | 2 | 1 < 2 | right = 5 |
| 4 | 5 | 4 | 0 | 1 | 0 < 1 | right = 4 |
| 4 | 4 | left === right | stop |
Answer: nums[4] = 0.
Why compare with the right end and not the left?
It's tempting to compare nums[mid] with nums[left]. But in a not rotated array like [1, 2, 3, 4], nums[mid] > nums[left] is always true, which would send you right, away from the minimum at index 0. Comparing with the right end works in every case.
The code
The reference solution is below, with comments on every important line.
How fast is it?
- Time: O(log n). Each step halves the range. For 5,000 elements, that's about 13 steps.
- Memory: O(1). Just a few indices.
Common mistakes
- Setting
right = mid - 1. Ifmidis the minimum, you just threw it away. Useright = mid. - Using
while (left <= right). Withright = mid, the range can stop shrinking whenleft === right, giving an infinite loop. Useleft < right. - Comparing with
nums[left]. Breaks on arrays that weren't rotated (see above).
Going deeper
The idea "binary search doesn't need a fully sorted array, just a way to tell which half to keep" is powerful. Search in Rotated Sorted Array uses the same trick to find any target, not just the minimum.
Reference solution
/**
* @param {number[]} nums
* @return {number}
*/
export default function findMin(nums) {
let left = 0
let right = nums.length - 1
// The minimum is always inside nums[left..right]. Stop when only one element is left.
while (left < right) {
const mid = Math.floor((left + right) / 2)
// mid is in the "high" run: the drop to the minimum is strictly to its right.
if (nums[mid] > nums[right]) left = mid + 1
// mid is in the "low" run: the minimum is mid or to its left (keep mid!).
else right = mid
}
return nums[left]
}export default function findMin(nums: number[]): number {
let left = 0
let right = nums.length - 1
// The minimum is always inside nums[left..right]. Stop when only one element is left.
while (left < right) {
const mid = Math.floor((left + right) / 2)
// mid is in the "high" run: the drop to the minimum is strictly to its right.
if (nums[mid] > nums[right]) left = mid + 1
// mid is in the "low" run: the minimum is mid or to its left (keep mid!).
else right = mid
}
return nums[left]
}def find_min(nums: list[int]) -> int:
left, right = 0, len(nums) - 1
# The minimum is always inside nums[left..right]. Stop when only one element is left.
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
# mid is in the "high" run: the drop to the minimum is strictly to its right.
left = mid + 1
else:
# mid is in the "low" run: the minimum is mid or to its left (keep mid!).
right = mid
return nums[left]