Find Minimum in Rotated Sorted Array
The array is a high run followed by a low run, and the minimum is where the low run starts (the "drop"). Compare nums[mid] with nums[hi]. If nums[mid] > nums[hi], mid is in the high run, so the drop is to the right: lo = mid + 1. Otherwise mid is already in the low run (or the array is sorted), so the minimum is at mid or to its left: hi = mid. Each step halves the range.
Within [lo, hi], nums[mid] > nums[hi] is true exactly when mid is before the drop. So the minimum is right of mid when it is true, and at mid or left of it when it is false.
Problem
Someone took a sorted array of distinct numbers (smallest to largest) and rotated it: they cut it at some point and moved the front part to the back. For example, [0, 1, 2, 4, 5, 6, 7] can become [4, 5, 6, 7, 0, 1, 2]. They may also have rotated it by 0 steps, so it can still be fully sorted.
You get the rotated array nums. Return its smallest element. Your solution must run in O(log n) time.
An integer array nums: a sorted array of distinct values, rotated 0 to n β 1 times.
The minimum value in nums.
The original array was [1, 2, 3, 4, 5], rotated 3 times. The values climb 3, 4, 5, then drop to 1. The value right after the drop is the minimum.
The drop is from 7 to 0, so 0 is the minimum.
Rotated 4 times (a full circle), so it is still sorted. There is no drop, and the minimum is the first element.
- β’ 1 β€ n β€ 5000
- β’ β5000 β€ nums[i] β€ 5000
- β’ All values are distinct.
- β’ nums is a sorted array rotated between 1 and n times (rotating n times gives back the sorted array).
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
If you draw the values as a line graph, what shape does a rotated sorted array make?
- 2.
Where exactly is the minimum in that shape?
- 3.
If I pick a middle element, can I tell whether it is in the 'high' part or the 'low' part of the array?
- 4.
Which element should I compare
nums[mid]with to answer that β and does it still work if the array was not rotated at all? - 5.
Could
miditself be the minimum? What does that mean for how I movehi?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Walk the whole array and keep the smallest value | O(N) | O(1) |
| Optimal | Compare nums[mid] with nums[hi]: bigger β go right, else keep mid and go left | O(log N) | O(1) |
Why does this belong to this pattern?
- βsorted array, then rotatedβNot fully sorted, but made of two sorted runs. Binary search still works if one check tells you which run mid is in.
- βfind the minimumβThere is no target. You are looking for a position β the drop β not a value.
- βO(log n)βA linear scan is ruled out. Halve the range each step.
- βdistinct valuesβ`nums[mid] == nums[hi]` only happens when mid == hi, so the two-way check is always clear.
This is the anchor of the rotated sub-pattern. The key idea: binary search does not need a target, it needs a yes/no question at mid that tells you which half to keep. Here the question is "is nums[mid] > nums[hi]?". Search in Rotated Sorted Array builds on this (find which half is sorted, then check the target), and Find Peak Element uses the same "which direction must the answer be in?" idea with a slope instead of a drop.
Don't make this mistake
For an array that was not rotated, like [1, 2, 3, 4, 5], nums[mid] > nums[lo] is true, so you move right and lose the minimum at index 0. nums[hi] is always in the low run, so comparing with it works for every rotation, including 0.
When nums[mid] < nums[hi], mid itself might be the minimum. In [4, 5, 1, 2, 3], mid = 2 points at 1, the answer. hi = mid β 1 throws it away. This loop is while (lo < hi) with hi = mid; don't mix it with the lo <= hi / hi = mid β 1 style.
When lo == hi, mid == hi and the else branch sets hi = mid again. Nothing changes, so the loop runs forever.
There is no target here, so there is nothing to compare nums[mid] with except the array itself. You are looking for the drop β the one place where a value is smaller than the one before it. If you catch yourself writing if (nums[mid] == target), you are solving Search in Rotated Sorted Array instead.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.