Find Minimum in Rotated Sorted Array - Problem

Imagine you have a perfectly sorted array that someone has rotated - like taking a portion from the end and moving it to the beginning. Your mission is to find the minimum element in this rotated sorted array.

For example, the sorted array [0,1,2,4,5,6,7] might become:

  • [4,5,6,7,0,1,2] if rotated 4 times
  • [6,7,0,1,2,4,5] if rotated 2 times
  • [0,1,2,4,5,6,7] if rotated 7 times (full rotation)

The twist? You must solve this in O(log n) time - no linear scanning allowed! This means you need to be smarter than checking every element.

Goal: Return the minimum element from the rotated sorted array.

Input & Output

example_1.py โ€” Basic rotation
$ Input: [3,4,5,1,2]
โ€บ Output: 1
๐Ÿ’ก Note: The original sorted array [1,2,3,4,5] was rotated 3 times to become [3,4,5,1,2]. The minimum element is 1.
example_2.py โ€” Heavy rotation
$ Input: [4,5,6,7,0,1,2]
โ€บ Output: 0
๐Ÿ’ก Note: The original sorted array [0,1,2,4,5,6,7] was rotated 4 times. The minimum element 0 is now in the middle of the array.
example_3.py โ€” No rotation
$ Input: [11,13,15,17]
โ€บ Output: 11
๐Ÿ’ก Note: This array is not rotated (or rotated by the full length). The minimum element is at the beginning.

Constraints

  • n == nums.length
  • 1 โ‰ค n โ‰ค 5000
  • -5000 โ‰ค nums[i] โ‰ค 5000
  • All elements in nums are unique
  • nums is sorted in ascending order and rotated between 1 and n times

Visualization

Tap to expand
4567012LEFTRIGHTMIDBinary Search: O(log n) - Eliminate half each timenums[mid] = 7 > nums[right] = 2, so minimum is in right halfMINFound!
Understanding the Visualization
1
Divide the array
Look at the middle element and compare with boundaries
2
Identify the sorted half
One half will be properly sorted, the other contains the rotation point
3
Choose the correct half
The minimum lies in the half containing the rotation point
4
Repeat until found
Continue halving until we pinpoint the minimum element
Key Takeaway
๐ŸŽฏ Key Insight: In a rotated sorted array, compare the middle with the rightmost element to determine which half contains the rotation point (and thus the minimum)
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28 Apple 22
89.5K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen