Skip to main content
17

Find Minimum in Rotated Sorted Array

Mediumbinary search

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

This problem uses:Binary SearchRead the beginner guide

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

text
Input:  nums = [3, 4, 5, 1, 2]
Output: 1

The original was [1, 2, 3, 4, 5], rotated 3 times.

Example 2

text
Input:  nums = [4, 5, 6, 7, 0, 1, 2]
Output: 0

Example 3

text
Input:  nums = [11, 13, 15, 17]
Output: 11

Not 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:

    text
    [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 is mid or 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):

    leftrightmidnums[mid]nums[right]CompareMove
    063727 > 2left = 4
    465121 < 2right = 5
    454010 < 1right = 4
    44left === rightstop

    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. If mid is the minimum, you just threw it away. Use right = mid.
    • Using while (left <= right). With right = mid, the range can stop shrinking when left === right, giving an infinite loop. Use left < 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

    js
    /**
     * @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]
    }
    ts
    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]
    }
    python
    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]