MediumBinary Search

Find Minimum in Rotated Sorted Array โ€” Solution

Problem

A sorted array with no duplicates has been rotated at some unknown pivot, so [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]. Given the rotated array, return its minimum element without sorting it.

  • Input: [4, 5, 6, 7, 0, 1, 2]
  • Output: 0
  • Explanation: The original sorted order was rotated starting at index 4, placing 0 in the middle of the array.

A non-rotated array like [1, 3, 5] is also valid โ€” the minimum is just the first element.

Intuition

Rotation splits the array into two sorted halves: a larger-valued left portion and a smaller-valued right portion. The minimum sits exactly at the transition between them. Because this structure exists, binary search can determine which half contains the transition point at every step, cutting the search space in half each time rather than scanning every element.

Approach 1 โ€” Linear Scan

Scan every element and track the smallest value found. This ignores the sorted-halves structure entirely.

  1. Initialize current_min to the first element.
  2. Iterate through remaining elements one by one.
  3. Update current_min whenever a smaller value is encountered.
  4. Return current_min after the full scan.
1def findMin(nums: list[int]) -> int:
2    current_min = nums[0]
3    for value in nums[1:]:
4        if value < current_min:
5            current_min = value
6    return current_min

Time: O(n) โ€” every element is visited exactly once.
Space: O(1) โ€” only a single tracking variable is needed.

Approach 2 โ€” Binary Search

Eliminate half the search space per step by exploiting the two-sorted-halves structure.

The key observation: if nums[mid] > nums[right], the left half is entirely greater than the right half's smallest element, so the minimum must be somewhere to the right of mid. Otherwise the minimum is at mid or to its left.

  1. Set left = 0, right = len(nums) - 1.
  2. While left < right, compute mid = left + (right - left) // 2.
  3. If nums[mid] > nums[right], the inflection point is right of mid โ€” set left = mid + 1.
  4. Otherwise the inflection point is at mid or left of it โ€” set right = mid (keep mid in range).
  5. When left == right, the pointers have converged on the minimum โ€” return nums[left].
1def findMin(nums: list[int]) -> int:
2    left, right = 0, len(nums) - 1
3
4    while left < right:
5        mid = left + (right - left) // 2
6
7        if nums[mid] > nums[right]:
8            # Left half is the "large" rotated portion; minimum is to the right
9            left = mid + 1
10        else:
11            # Mid could be the minimum itself, so don't exclude it
12            right = mid
13
14    return nums[left]

Time: O(log n) โ€” the search space halves on every iteration.
Space: O(1) โ€” only two index pointers are maintained.

Complexity Summary

ApproachTimeSpaceWhen to use
Linear ScanO(n)O(1)Array is small or unsorted โ€” no structure to exploit
Binary SearchO(log n)O(1)Input is guaranteed to be a rotated sorted array with no duplicates

Common Mistakes

  • Comparing mid to left instead of right โ€” comparing against the left boundary doesn't reliably identify which half contains the minimum, because the left element may or may not be in the "large" rotated portion. The right boundary is always in the smaller portion, making it the correct reference point.
  • Using right = mid - 1 instead of right = mid โ€” when nums[mid] <= nums[right], mid itself might be the minimum. Excluding it with mid - 1 can skip the answer entirely.
  • Initializing right = len(nums) instead of len(nums) - 1 โ€” this causes an out-of-bounds access when computing nums[right] inside the loop.
  • Using left <= right as the loop condition โ€” this results in an infinite loop when left == right because neither branch changes both pointers when they're equal.
  • Integer overflow on mid calculation โ€” (left + right) / 2 can overflow in Java and C++ with large indices. Always use left + (right - left) / 2.

Related Problems

Ready to practice? Try it on SkillFlow

Adaptive problems, AI follow-up interviews, and a skill score that shows exactly where you need to improve.

Practice This Problem โ†’