15% off all plans & roadmaps Use code

See plans

Find Minimum in Rotated Sorted Array

Updated

TL;DR: A sorted array of unique numbers was rotated, and you must return its smallest value in O(log n). Binary search works if you compare the middle element with the right end to decide which half holds the drop.

Problem

A sorted array of length n in ascending order has been rotated between 1 and n times. For example, nums = [0,1,2,4,5,6,7] can become:

  • [4,5,6,7,0,1,2] after 4 rotations.
  • [0,1,2,4,5,6,7] after 7 rotations.

One rotation turns [a[0], a[1], a[2], ..., a[n-1]] into [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

You are given such a rotated array nums whose elements are all unique. Return its minimum element. Your algorithm must run in O(log n) time.

Example 1

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5], rotated 3 times.

Example 2

Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7], rotated 4 times.

Example 3

Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17], rotated 4 times.

Constraints

  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • All integers of nums are unique.
  • nums is sorted and rotated between 1 and n times.

Constraints that matter

  • The statement demands O(log n) time, so a full scan is rejected by the rules even though n is only 5000. The interviewer wants binary search.
  • All values are unique, so nums[mid] never equals nums[right] when mid < right. You need no tie-breaking branch.
  • The array is rotated between 1 and n times, and n rotations leave it fully sorted. Your search must handle the unrotated case, where the minimum sits at index 0.
  • n can be 1, so the loop must tolerate a single element without reading outside the array.
  • n up to 5000 means an O(n log n) sort would pass the tests, but it still breaks the stated time limit. Values fit in a 32-bit int, so overflow is not a concern for the values, only a habit for the midpoint.

View on LeetCode

Hints

Hint 1

A rotated sorted array is two ascending blocks glued together.

Hint 2

The most direct idea ignores the structure.

Solution

Intuition

A rotated sorted array is two ascending blocks glued together. The left block holds the larger values and the right block holds the smaller ones. The minimum is the first element of the right block, and it is the only element smaller than its left neighbour.

Picture a sorted row of books that someone slid so the last few books now sit at the front. The right end of the range is the fixed reference book, and each probe checks whether the middle book sits above or below it, which tells you which side the break is on.

A normal binary search needs a target to compare against. Here you have none, so you compare nums[mid] with the last element of the current range, nums[right]. The right end always belongs to the smaller block, or the range is fully sorted. That makes it a stable reference point.

If nums[mid] > nums[right], then mid sits in the big left block and the drop lies after mid. The minimum is strictly to the right. If nums[mid] < nums[right], the stretch from mid to right is ascending, so nothing after mid can be smaller. The minimum is mid or something to its left.

Each comparison throws away about half the range, which gives O(log n). The problem gives you no target value. Whenever you must find a boundary or extreme in a structure with one sharp break, ask which half the break lives in and compare against a fixed end.

Watch the pattern animation

Worked example

Input: nums = [2, 3, 4, 5, 6, 7, 8, 9, 1]
Output: 1
Explanation: The original sorted array was [1, 2, 3, 4, 5, 6, 7, 8, 9]. One rotation moved the 1 from the back to the front of the array... and then the array was rotated so that 1 ended at the last index. The values 2 through 9 form the ascending left block, and the lone 1 forms the right block, so the minimum is 1.

Approach 1: Sort, then take the first element

The most direct idea ignores the structure. The minimum of any collection is the first element once it is sorted, so sort a copy of the array and read index 0.

This works for any input, rotated or not. It throws away the fact that the array is already almost sorted, and it pays O(n log n) to rebuild an order you mostly had.

Steps

  1. Copy nums so the caller's array is not changed.
  2. Sort the copy in ascending order.
  3. Return the element at index 0 of the sorted copy.

Dry run

Start with [2, 3, 4, 5, 6, 7, 8, 9, 1].

  • Copy the array and sort it. The copy becomes [1, 2, 3, 4, 5, 6, 7, 8, 9].
  • Read index 0. The value is 1.
  • Return 1, which matches the expected output.

Complexity

TimeO(n log n)
SpaceO(n)

Here n is the length of nums. A comparison sort does O(n log n) comparisons on n elements. The copy holds n values, so the extra space is O(n). Some library sorts need extra working memory too, but the copy already dominates.

PythonTested in our runner

from typing import List, Optional

class Solution:
    def findMin(self, nums: List[int]) -> int:
        # sorted() builds a new list, so nums is untouched
        return sorted(nums)[0]

Approach 2: Linear scan

What's slow: sorting orders every element when you only need one value. Ordering costs O(n log n) and an extra copy.

The insight that fixes it: a minimum needs a single pass. Keep the smallest value seen so far and compare each element against it. This needs no extra memory and drops the log factor. It still reads every element, so it cannot meet the O(log n) requirement.

Steps

  1. Set best to nums[0].
  2. Walk the array from index 1 to the end.
  3. If the current element is smaller than best, store it in best.
  4. Return best after the last element.

Dry run

Start with [2, 3, 4, 5, 6, 7, 8, 9, 1].

  • best = 2 from index 0.
  • Indices 1 to 7 hold 3, 4, 5, 6, 7, 8, 9. None is below 2, so best stays 2.
  • Index 8 holds 1. Since 1 < 2, best = 1.
  • The loop ends and the function returns 1.

Complexity

TimeO(n)
SpaceO(1)

Here n is the length of nums. The loop body runs n - 1 times and each iteration does one comparison, so time is O(n). The only extra storage is the variable best, so space is O(1). The scan never exploits the sorted structure, which is why it cannot beat linear time.

PythonTested in our runner

from typing import List, Optional

class Solution:
    def findMin(self, nums: List[int]) -> int:
        best = nums[0]
        for x in nums[1:]:
            if x < best:
                best = x
        return best
Find Min/Max in Rotated Sorted Array pattern infographic for Find Minimum in Rotated Sorted Array

Solution pattern

Find Min/Max in Rotated Sorted Array

Part of Binary Search Patterns

Approach 3: Binary search against the right endOptimalFind Min/Max in Rotated Sorted Array

What's slow: the linear scan inspects every element, including the ones the sorted structure already rules out. After one comparison in the middle you can tell which half cannot hold the minimum.

The insight that fixes it: compare nums[mid] with nums[right]. If nums[mid] > nums[right], the drop from large to small happens after mid, so move left = mid + 1. Otherwise the stretch mid..right is ascending, so the minimum is mid or to its left, and you set right = mid. Keep mid in the range on that branch, since it may be the answer.

Loop while left < right. When the two pointers meet, one candidate remains and it is the minimum.

Steps

  1. Set left = 0 and right = len(nums) - 1.
  2. While left < right, compute mid = left + (right - left) / 2 with integer division.
  3. If nums[mid] > nums[right], the minimum is right of mid, so set left = mid + 1.
  4. Otherwise the minimum is at mid or left of it, so set right = mid.
  5. When the loop ends, left == right. Return nums[left].

Dry run

Trace on [2, 3, 4, 5, 6, 7, 8, 9, 1], where n = 9 and the final answer is 1.

Stepleftrightmidnums[mid]nums[right]Decision
start08range is the whole array
1084616 > 1, so left = 5
2586818 > 1, so left = 7
3787919 > 1, so left = 8
end88left == right, return nums[8] = 1

The range shrank from 9 elements to 6, then 4, then 2, then 1. Every middle value was larger than the right end of 1, so the search kept moving right toward the drop and landed on the minimum 1.

Complexity

TimeO(log n)
SpaceO(1)

Here n is the length of nums. With a range of size s, the new range has at most ceil(s / 2) elements: left = mid + 1 keeps the upper part, and right = mid keeps the lower part including mid. Halving from n down to 1 takes about log2(n) iterations, which is at most 13 for n = 5000. Each iteration does constant work and the code stores only three integers, so space is O(1).

PythonTested in our runner

from typing import List, Optional

class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] > nums[right]:
                # drop is after mid, so the minimum is on the right
                left = mid + 1
            else:
                # mid..right is ascending, mid may be the minimum
                right = mid
        return nums[left]

Common mistakes

  • Writing while left <= right together with right = mid. When left == right the body keeps setting right = mid to the same value and never exits. Use while left < right.
  • Setting right = mid - 1 in the else branch. The element at mid can be the minimum, for example in [3, 1, 2] after the first step, so this discards the answer. Use right = mid.
  • Comparing nums[mid] with nums[left] to pick a side. On an unrotated array like [1, 2, 3], nums[mid] > nums[left] is true but the minimum is on the left. Compare with nums[right] instead, which works for every rotation.
  • Writing left = mid in the first branch. With left < right, mid can equal left, so the range stops shrinking and the loop hangs. Use left = mid + 1, which is safe because nums[mid] is larger than nums[right] and cannot be the minimum.
  • Looking for a drop with nums[i] > nums[i + 1] and returning nums[i + 1]. On an array rotated n times, no drop exists, so the code falls through. The comparison against nums[right] needs no special case.
  • Using nums[0] as a default return when nothing is found, or skipping the single-element case. For [5] the loop never runs and nums[left] must still be returned. Return nums[left] after the loop in all cases.

Edge cases

  • [1]: one element, the loop is skipped, and the answer is 1.
  • [1, 2, 3, 4, 5]: rotated n times, so it is fully sorted. Every middle is below the right end, right keeps moving to mid, and the answer is 1 at index 0.
  • [2, 1]: two elements. mid = 0, nums[0] = 2 > nums[1] = 1, so left = 1 and the answer is 1.
  • [5, 1, 2, 3, 4]: the minimum is at index 1 right after the first element. The search must not skip it, and returns 1.
  • [2, 3, 4, 5, 6, 7, 8, 9, 1]: the minimum is the last element. The search moves right every time and ends at index 8.
  • Negative values such as [-1, 0, 3, -5]: comparisons work the same on negatives, and the answer is -5.

Follow-ups interviewers ask

What if the array can contain duplicates?
Then nums[mid] == nums[right] becomes possible. You cannot tell which side holds the minimum, so you shrink the range with right -= 1. That is safe because the value you drop equals nums[mid], which stays in the range. The average case is still logarithmic, but an array of all equal values forces O(n) in the worst case.
How do you find how many times the array was rotated?
Run the same search and return the index left instead of the value. The minimum's index equals the number of rotations modulo n, where an index of 0 means the array is unrotated. The cost stays O(log n).
How would you search for a target in a rotated sorted array?
Find the pivot index with this search, then binary search the left block or the right block depending on where the target falls. You can also do it in one pass by checking which half is sorted at each step. Both options are O(log n).

Find Minimum in Rotated Sorted Array FAQ

What is the time complexity of Find Minimum in Rotated Sorted Array?

The optimal solution is O(log n) time and O(1) space, where n is the array length. Each iteration discards about half of the remaining range. A linear scan is O(n) and sorting is O(n log n), and neither meets the required bound.

Why use binary search for Find Minimum in Rotated Sorted Array?

The array is two ascending blocks with one drop, so a single comparison between nums[mid] and nums[right] tells you which half contains the drop. That halves the search range every step. The problem also states the O(log n) requirement, which only binary search can reach here.

Why compare nums[mid] with nums[right] and not nums[left]?

The right end is either in the smaller block or the whole range is sorted, so comparing against it gives a clean decision in all cases. Comparing with nums[left] is ambiguous when the range is already sorted, because the minimum is then at left while nums[mid] > nums[left] suggests moving right.

Can Find Minimum in Rotated Sorted Array be solved in O(1) extra space?

Yes. The binary search keeps only left, right and mid, so extra space is O(1). The sorting approach needs a copy and uses O(n).

Why is it right = mid and not right = mid - 1?

When nums[mid] < nums[right], the element at mid may be the minimum itself. Setting right = mid - 1 could throw it away. Keeping mid in the range is safe, and the loop still ends because mid < right whenever left < right.

Does the same code handle an array that was not rotated?

Yes. A fully sorted array is the case of n rotations. Every nums[mid] is below nums[right], so right slides left to index 0 and the function returns nums[0].

Asked at

See which companies ask this — Pro

Practise it