Find Minimum in Rotated Sorted Array
The minimum is the only element smaller than its predecessor — binary search the rotation point.
Approach
Compare nums[mid] to nums[hi]. If nums[mid] > nums[hi], the pivot (minimum) is to the right, so move lo up; otherwise it's at mid or to the left, so move hi down. Converge until lo == hi, which is the minimum.
Time complexity
O(log n)
Space complexity
O(1)
Common mistake
Comparing to nums[lo] instead of nums[hi] — the comparison to the right boundary is what stays correct under rotation.
Interactive walkthrough — step through it below, free and without an account
Problem
You are given an ascending array of distinct integers that may have been rotated around an unknown position. Return the smallest value in the array while using a logarithmic-time strategy. The array may also be unrotated, in which case its first element is the minimum.
Examples
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Input: nums = [2,3,4,5,1]
Output: 1
Problem
Problem
You are given an ascending array of distinct integers that may have been rotated around an unknown position. Return the smallest value in the array while using a logarithmic-time strategy. The array may also be unrotated, in which case its first element is the minimum.
Problem and Goal
Learning goal: Recognize why the minimum in a rotated sorted array can be found without checking every element.
Core idea A sorted array has no downward step: every value is at least as large as the one before it. Rotation creates exactly one such break, where the minimum follows the maximum. Finding that break is equivalent to finding the minimum.
The input contains distinct values, so the minimum is unambiguous. Our goal is to locate its index in O(log n) time and return the value at that index.
Algorithm plan
- Keep a search interval from
lothroughhi. - Inspect
mid, the middle index. - Compare
nums[mid]withnums[hi]to decide which half contains the rotation point. - Stop when
lo == hi.
Micro-example
For [4,5,6,7,0,1,2], the downward break is 7 → 0.
The answer is therefore the value at the rotation point: 0.
Pitfalls
- Do not linearly scan for the downward step; that loses the logarithmic bound.
- The array can be rotated zero times, so the first element may already be the minimum.
- Use
nums[hi]as the comparison boundary, notnums[lo].
Recap
- Rotation creates one pivot where the minimum begins.
- Binary search can isolate that pivot.
- The final index
lo == histores the minimum.
Problem and Goal
The rotation point is the minimum
In [4, 5, 6, 7, 0, 1, 2], the only downward transition is 7 → 0. That successor, 0, is the minimum.
Search for the pivot instead of scanning every value.
Key Idea: Compare with the Right Boundary
Learning goal: Understand why comparing nums[mid] with nums[hi] identifies the correct half.
Core idea
The right boundary tells us which sorted section contains mid. If nums[mid] > nums[hi], then mid lies in the left, larger section of the rotation. The minimum must be strictly to the right of mid, so we can set lo = mid + 1.
Otherwise, nums[mid] <= nums[hi]. The suffix from mid to hi is ordered, which means the pivot is at mid or somewhere to its left. We preserve mid by setting hi = mid.
Algorithm plan
- Initialize
lo = 0andhi = n - 1. - Compute
mid = lo + (hi - lo) // 2. - If
nums[mid] > nums[hi], discardlo..midand movelotomid + 1. - Otherwise, keep
midand movehitomid. - Return
nums[lo]when the interval collapses.
Micro-example
With mid = 3, nums[mid] = 7, and nums[hi] = 2, 7 > 2.
Therefore the pivot is right of index 3, so lo becomes 4.
Pitfalls
- When
nums[mid] <= nums[hi], do not usehi = mid - 1;midmay be the answer. - The comparison is directional:
>means move right, otherwise move left or stay. - Distinct values make equality safe in the second branch.
Recap
nums[mid] > nums[hi]means the pivot is right ofmid.- Otherwise, the pivot is at
midor left of it. - Comparing with the right boundary keeps this reasoning valid under rotation.
Key Idea: Compare with the Right Boundary
If nums[mid] > nums[hi], the middle is in the larger left section, so the minimum lies strictly to the right.
7 > 2 ⇒ lo = mid + 1
Main Approach and Correctness
Learning goal: Turn the comparison rule into a safe binary-search invariant.
Core idea
Maintain the invariant that the minimum is always inside the closed interval [lo, hi]. Each comparison discards a region that cannot contain the pivot, while the branch hi = mid deliberately retains mid because it may be the minimum.
The search never needs to identify the predecessor explicitly. Instead, the relative order between nums[mid] and the rightmost value reveals whether the interval crosses the rotation point.
Algorithm plan
- Start with the whole array as the candidate interval.
- Repeatedly choose
mid. - If
nums[mid] > nums[hi], setlo = mid + 1. - Otherwise set
hi = mid. - When one index remains, return
nums[lo].
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = mid
return nums[lo]Micro-example
The interval [0, 6] becomes [4, 6], then [4, 5], then [4, 4].
The invariant guarantees that index 4 still contains the pivot throughout.
Pitfalls
- Use
while lo < hi, so the final one-element interval is not discarded. - Use
mid + 1only in the>branch, wheremidis proven not to be the minimum. - Keep the interval closed; both endpoints are candidates.
Recap
- The invariant is “the minimum lies in
[lo, hi].” - Every iteration removes at least half the remaining candidates.
- Retaining
midin the second branch ensures correctness.
Main Approach and Correctness
| Approach | Time | Space | Core decision |
|---|---|---|---|
| Binary search | O(log n) | O(1) | Compare nums[mid] with nums[hi] |
| Linear scan | O(n) | O(1) | Check each value for the minimum |
Walkthrough: Locate 0 Step by Step
Learning goal: Trace every interval update on the example [4,5,6,7,0,1,2].
Core idea
We begin with every index as a candidate. At each step, nums[hi] acts as the reference value for the right sorted portion. A middle value larger than that reference proves that the rotation point is farther right; a middle value no larger than it lets us keep the middle and search left.
The important distinction is whether mid can still be the answer. In the > case it cannot, so we move past it. In the other case it can, so hi moves onto mid rather than before it.
Algorithm plan
- Check indices 0 through 6.
- Compare index 3 with index 6:
7 > 2, so search indices 4 through 6. - Compare index 5 with index 6:
1 <= 2, so search indices 4 through 5. - Compare index 4 with index 5:
0 <= 1, so search index 4. - Return
nums[4] = 0.
Micro-example
The intervals shrink as [0,6] → [4,6] → [4,5] → [4,4].
The final candidate is index 4, where the minimum value is 0.
Pitfalls
- A sorted-looking right half does not mean the minimum is at
hi; it may be atmidor left. - Record the updated interval after each comparison, not before it.
- The answer is a value, while the binary search primarily tracks an index.
Recap
- Each decision preserves index 4 as a candidate.
- The interval reaches one index after three comparisons.
- The traced result is
0.
Walkthrough: Locate 0 Step by Step
Animates the exact binary-search trace for the lesson's first example.
Pitfalls and Edge Cases
Learning goal: Avoid the boundary mistake that makes this binary search unreliable.
Core idea
The right boundary is special because it lets us determine whether mid belongs to the rotated, larger section or the sorted suffix. Comparing with nums[lo] does not provide the same stable signal: the left endpoint may itself be in either section, and its relationship with mid can be ambiguous after rotation.
The algorithm also handles unrotated arrays. If the array is [1,2,3,4], every middle value is less than the right boundary, so hi moves left until index 0 remains.
Algorithm plan
- Compare only
nums[mid]andnums[hi]. - For
nums[mid] > nums[hi], uselo = mid + 1. - Otherwise, use
hi = mid. - Test one-element and unrotated arrays explicitly.
Micro-example
For [1,2,3,4], 3 <= 4, then 2 <= 3, then 1 <= 2.
The interval converges at index 0, returning 1.
Pitfalls
- Do not compare to
nums[lo]instead ofnums[hi]. The right-boundary comparison stays correct under rotation. - Do not decrement
hiin the second branch;midmay hold the minimum. - Do not assume the minimum is always near the middle or at an endpoint.
Recap
- The right boundary is the reliable reference.
hi = midpreserves a possible answer.- Zero rotation and one-element arrays require no special code.
Pitfalls and Edge Cases
For a rotated sorted array, which rule is correct?
Complexity and Wrap-up
Learning goal: Connect interval halving to the target complexity and implement the final solution confidently.
Core idea
Each iteration removes a portion of the candidate interval while preserving the minimum. Since the interval is repeatedly reduced by about half, the number of comparisons grows logarithmically with n. The algorithm stores only a few indices, so its auxiliary memory remains constant.
The returned value is nums[lo] after convergence. No separate pivot search, sorting step, or predecessor check is necessary.
Algorithm plan
- Set
lo = 0andhi = n - 1. - While
lo < hi, calculatemid. - Compare
nums[mid]tonums[hi]and keep the valid half. - Return
nums[lo].
Micro-example
For seven values, the candidate counts go from 7 to 3 to 2 to 1.
That shrinking pattern is the signature of O(log n) time.
Pitfalls
- Do not sort the input: sorting costs
O(n log n)and discards the rotation structure. - Do not allocate a second array; it is unnecessary.
- Remember that
lois an index during the search andnums[lo]is the final answer.
Recap
- Time complexity:
O(log n). - Extra space:
O(1). - The converged boundary is the rotation point and minimum.
Complexity and Wrap-up
Shows the logarithmic search cost and constant auxiliary space.
Now try it on your own problem
Paste any LeetCode problem — or one of your own — and codebrew builds a walkthrough like this one for it.
Build a lesson from any problem
