Day 40 of 90MediumLeetCode

Find Minimum in Rotated Sorted Array

A sorted array of distinct numbers was rotated (its tail moved to the front); find its smallest element in O(log n) time.

Pattern
Binary search on a rotation predicate
Time box
25 min
Time
O(log n)
Space
O(1)

Try it first — give it 25 minutes

0/4 revealed

Stuck? 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.

    i0123456
    nums[i]4567012
    nums[i] <= 2?FFFFTTT

    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

    1. Let last = nums[n - 1].
    2. Set lo = 0 and hi = n - 1. The last index is always true (last <= last), so hi starts on a known true. No past-the-end slot is needed here.
    3. While lo < hi:
      • mid = lo + (hi - lo) // 2.
      • If nums[mid] <= last, mid is in the second staircase, so the minimum is mid or to its left: hi = mid.
      • Otherwise mid is in the first staircase, and the minimum is strictly to the right: lo = mid + 1.
    4. 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]

    Dry run

    nums = [4, 5, 6, 7, 0, 1, 2], so last = 2, lo = 0, hi = 6:

    lomidhinums[mid]<= 2?update
    0367Nolo = 4
    4561Yeshi = 5
    4450Yeshi = 4
    44stop

    Return nums[4] = 0.

    Not rotated, nums = [11, 13, 15, 17], so last = 17:

    lomidhinums[mid]<= 17?update
    01313Yeshi = 1
    00111Yeshi = 0
    00stop

    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 = 0 from the start; the loop doesn't run, and you return nums[0].
    • Two elements, like [2, 1]. last = 1. mid = 0, 2 <= 1 is false, so lo = 1. Return 1.
    • hi = mid - 1 when nums[mid] <= last. mid might 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 run mid is in. The usual fix: compare with nums[hi], and when they're equal, do hi -= 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 (mod n).
    • "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