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
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)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code