Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
Take a sorted array of distinct numbers, like [0, 1, 2, 4, 5, 6, 7], and rotate it: move some number of elements from the end to the front, giving say [4, 5, 6, 7, 0, 1, 2]. It may have been rotated anywhere from 1 to n times, and rotating n times gives the original sorted array back.
You get the rotated array. Return its minimum element, in O(log n) time. Up to 5000 elements.
Example: nums = [3, 4, 5, 1, 2] → 1. This was [1, 2, 3, 4, 5] rotated 3 times.
Start with brute force
Scan the array and keep the smallest value: O(n). A slightly smarter scan finds the one place where nums[i] > nums[i + 1]; the minimum is nums[i + 1]. Still O(n).
That's 5000 steps here, which is fine for a computer, but the problem asks for O(log n), about 13 steps. A scan doesn't use the fact that the array is almost sorted.
Building the intuition
Draw [4, 5, 6, 7, 0, 1, 2] as a graph. It's two staircases: one climbs from 4 to 7, then it drops, and a second one climbs from 0 to 2. The minimum is the bottom of the second staircase.
Binary search needs a yes/no question that's false and then true. The array isn't sorted, so nums[i] >= x won't work. Is there something that tells the two staircases apart?
Look at the last element, 2. The whole second staircase ends at 2, so all of it is <= 2. The first staircase starts above where the second one ends (that's how rotation works), so all of it is > 2.
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| nums[i] | 4 | 5 | 6 | 7 | 0 | 1 | 2 |
| nums[i] <= 2? | F | F | F | F | T | T | T |
Falses, then trues, and the first true is the minimum. So this is the primer's template again, with a new question.
What if the array wasn't rotated at all, like [11, 13, 15, 17]? Then everything is <= 17, so it's all T, the first T is index 0, and nums[0] = 11 is the minimum. No special case needed.
The optimal approach
- Let
last = nums[n - 1]. - Set
lo = 0andhi = n - 1. The last index is always true (last <= last), sohistarts on a known true. No past-the-end slot is needed here. - While
lo < hi:mid = lo + (hi - lo) // 2.- If
nums[mid] <= last,midis in the second staircase, so the minimum ismidor to its left:hi = mid. - Otherwise
midis in the first staircase, and the minimum is strictly to the right:lo = mid + 1.
- Return
nums[lo].
The subtle detail: compare with the last element, not the first. "Is nums[i] < nums[0]?" also splits the two staircases. But when the array isn't rotated, it's false everywhere, so the first true doesn't exist and you need an extra special case. Comparing with the last element makes the unrotated case just "all true".
You may also see a version that compares nums[mid] with nums[hi] instead of the fixed last element. That's also correct, but the fixed last makes the yes/no question visible, which is the point of this stage.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def findMin(self, nums: List[int]) -> int:
# Everything before the drop is > last; the minimum and everything after it are <= last.
# So "nums[i] <= last" goes F F F T T, and the first T is the minimum.
last = nums[-1]
lo, hi = 0, len(nums) - 1 # the last index is always T, so hi starts on a T
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[mid] <= last:
hi = mid # mid is in the second run: the minimum is here or left
else:
lo = mid + 1 # mid is in the first run: the minimum is to the right
return nums[lo]class Solution {
public:
int findMin(vector<int>& nums) {
// Everything before the drop is > last; the minimum and everything after it are <= last.
// So "nums[i] <= last" goes F F F T T, and the first T is the minimum.
int last = nums.back();
int lo = 0, hi = (int)nums.size() - 1; // the last index is always T
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] <= last) {
hi = mid; // mid is in the second run: the minimum is here or left
} else {
lo = mid + 1; // mid is in the first run: the minimum is to the right
}
}
return nums[lo];
}
};class Solution {
public int findMin(int[] nums) {
// Everything before the drop is > last; the minimum and everything after it are <= last.
// So "nums[i] <= last" goes F F F T T, and the first T is the minimum.
int last = nums[nums.length - 1];
int lo = 0, hi = nums.length - 1; // the last index is always T
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] <= last) {
hi = mid; // mid is in the second run: the minimum is here or left
} else {
lo = mid + 1; // mid is in the first run: the minimum is to the right
}
}
return nums[lo];
}
}/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function (nums) {
// Everything before the drop is > last; the minimum and everything after it are <= last.
// So "nums[i] <= last" goes F F F T T, and the first T is the minimum.
const last = nums[nums.length - 1];
let lo = 0, hi = nums.length - 1; // the last index is always T
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (nums[mid] <= last) {
hi = mid; // mid is in the second run: the minimum is here or left
} else {
lo = mid + 1; // mid is in the first run: the minimum is to the right
}
}
return nums[lo];
};Dry run
nums = [4, 5, 6, 7, 0, 1, 2], so last = 2, lo = 0, hi = 6:
| lo | mid | hi | nums[mid] | <= 2? | update |
|---|---|---|---|---|---|
| 0 | 3 | 6 | 7 | No | lo = 4 |
| 4 | 5 | 6 | 1 | Yes | hi = 5 |
| 4 | 4 | 5 | 0 | Yes | hi = 4 |
| 4 | 4 | stop |
Return nums[4] = 0.
Not rotated, nums = [11, 13, 15, 17], so last = 17:
| lo | mid | hi | nums[mid] | <= 17? | update |
|---|---|---|---|---|---|
| 0 | 1 | 3 | 13 | Yes | hi = 1 |
| 0 | 0 | 1 | 11 | Yes | hi = 0 |
| 0 | 0 | stop |
Return nums[0] = 11.
Complexity
- Time: O(log n). Each step halves
[lo, hi]: about 13 steps for 5000 elements. - Space: O(1). A few integers.
Edge cases and traps
- Not rotated. All true, so the answer is index 0. Comparing with
nums[0]instead would break here. - One element.
lo = hi = 0from the start; the loop doesn't run, and you returnnums[0]. - Two elements, like
[2, 1].last = 1.mid = 0,2 <= 1is false, solo = 1. Return 1. hi = mid - 1whennums[mid] <= last.midmight be the minimum itself. In the first dry run, that would skip index 4 and return 1 instead of 0.- Duplicates. This problem promises distinct values. With duplicates the question isn't monotonic any more; see the follow-up.
Say it in the interview
"The rotated array is two increasing runs, and the minimum is the start of the second run. Every element of the second run is at most the last element, and every element of the first run is bigger than it. So 'is nums[i] at most the last element?' is false and then true, and I binary search for the first true, with hi starting at the last index because that's always true. If the array isn't rotated, everything is true and I get index 0. O(log n) time, O(1) space."
Follow-ups interviewers ask
- "What if there are duplicates?" That's Find Minimum in Rotated Sorted Array II. With
[1, 1, 1, 0, 1]you can't tell which runmidis in. The usual fix: compare withnums[hi], and when they're equal, dohi -= 1. It's still correct, but worst case O(n). - "How many times was it rotated?" The index of the minimum,
lo, is the rotation count (modn). - "Now search for a target." Find the minimum first, then binary search the half that can hold the target. That's Search in Rotated Sorted Array, next on the path.
Practise the same idea
This is Day 40 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free