Find Minimum in Rotated Sorted Array II - Problem

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become:

  • [4,5,6,7,0,1,4] if it was rotated 4 times.
  • [0,1,4,4,5,6,7] if it was rotated 7 times.

Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.

You must decrease the overall operation steps as much as possible.

Input & Output

Example 1 — Basic Rotation
$ Input: nums = [1,3,5]
Output: 1
💡 Note: The array is not rotated, minimum is the first element: 1
Example 2 — With Duplicates
$ Input: nums = [2,2,2,0,1]
Output: 0
💡 Note: Array has duplicates, but minimum is still 0 which appears after the duplicates
Example 3 — All Same Elements
$ Input: nums = [3,3,3,3,3]
Output: 3
💡 Note: All elements are the same, so minimum is 3

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 5000
  • -5000 ≤ nums[i] ≤ 5000
  • nums is sorted and rotated between 1 and n times.
  • nums may contain duplicates

Visualization

Tap to expand
Find Minimum in Rotated Sorted Array with DuplicatesOriginal: [0,1,4,4,5,6,7] → Rotated: [4,5,6,7,0,1,4]4567014MINDUPLICATEChallenge: Duplicates make it harder to determine search directionSolution: When nums[mid] == nums[right], reduce right by 1Minimum Element: 0
Understanding the Visualization
1
Input
Sorted array rotated with possible duplicates
2
Process
Use binary search but handle duplicate elements
3
Output
Return the minimum element
Key Takeaway
🎯 Key Insight: When duplicates prevent binary search direction determination, we must fall back to linear reduction
Asked in
Amazon 45 Microsoft 38 Google 32
125.0K Views
Medium Frequency
~25 min Avg. Time
2.9K 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