LeetCode Problem

How to Solve Find Minimum in Rotated Sorted Array II

The problem requires finding the minimum element in a rotated sorted array, which may contain duplicates. This can be efficiently solved using binary search, adjusting the search conditions to handle the presence of duplicates. The main challenge is to deal with rotations and duplicates while maintaining the efficiency of binary search.

GhostInterview Help

Need help with Find Minimum in Rotated Sorted Array II without spending extra time grinding it?

GhostInterview can read Find Minimum in Rotated Sorted Array II from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #154Binary search over the valid answer spaceReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Binary search over the valid answer space
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem requires finding the minimum element in a rotated sorted array, which may contain duplicates. This can be efficiently solved using binary search, adjusting the search conditions to handle the presence of duplicates. The main challenge is to deal with rotations and duplicates while maintaining the efficiency of binary search.

Problem Statement

You are given a sorted array that has been rotated at some unknown pivot. The array may contain duplicates, making it challenging to apply standard binary search. Your task is to find the minimum element in this rotated sorted array.

For example, the array nums = [2,2,2,0,1] has been rotated, and the minimum value is 0. The array is guaranteed to contain at least one element, and the length will be between 1 and 5000.

Examples

Example 1

Input: nums = [1,3,5]

Output: 1

Example details omitted.

Example 2

Input: nums = [2,2,2,0,1]

Output: 0

Example details omitted.

Constraints

  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • nums is sorted and rotated between 1 and n times.

Solution Approach

Binary Search Approach

Use a modified binary search to divide the array into two halves. Compare the middle element with the left and right elements to decide which half to search. This ensures the algorithm works even with duplicates, where you may need to reduce the search space by skipping over duplicate values.

Handling Duplicates

When duplicates are encountered, it can be difficult to distinguish which half of the array contains the minimum. In such cases, rather than eliminating half of the array, shrink the search space by incrementing or decrementing the search boundaries until you find a unique candidate for the minimum.

Time Complexity Considerations

The solution operates in O(n) time in the worst case due to the handling of duplicates. In the average case, the time complexity can be O(log n), depending on how effectively the search space is reduced in each iteration.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The worst-case time complexity is O(n) due to the need to handle duplicates, where every element might be checked. In the best case, the complexity can be reduced to O(log n) by effectively splitting the array using binary search. The space complexity is O(1) as no extra space is required beyond the input array.

What Interviewers Usually Probe

  • Tests how the candidate handles the trade-off between efficiency and correctness when dealing with duplicates in binary search.
  • Assesses the ability to modify standard algorithms (binary search) to handle edge cases, such as duplicate values and rotations.
  • Evaluates the candidate's ability to reason about the problem and identify when and why to adjust search boundaries in a rotated array.

Common Pitfalls or Variants

Common pitfalls

  • Misunderstanding the handling of duplicates, leading to inefficient solutions that don't maintain the correct time complexity.
  • Failing to recognize when the search space should be reduced due to duplicate values, causing the algorithm to behave like a linear search in the worst case.
  • Not properly handling edge cases where the array is very small or where the array contains multiple equal elements near the pivot point.

Follow-up variants

  • Rotate the array more times (larger rotations), requiring better insight into how the array structure behaves after multiple rotations.
  • Use this pattern to find the maximum value instead of the minimum by reversing the logic of the binary search.
  • Implement an iterative approach rather than a recursive binary search, which might better handle larger inputs with fewer function calls.

How GhostInterview Helps

  • Provides immediate feedback on the effectiveness of the binary search strategy, especially in handling edge cases like duplicates.
  • Guides you through the thought process of narrowing down the search space based on comparisons of the middle element, which is critical for avoiding inefficiencies.
  • Simulates real-time interview conditions to help refine your problem-solving approach, focusing on efficient binary search use and troubleshooting pitfalls.

Topic Pages

FAQ

How do I handle duplicates in this problem?

To handle duplicates, you may need to adjust the boundaries of your binary search, reducing the search space incrementally when duplicates are encountered. If duplicates are on both sides of the middle element, shift your search boundaries inward to avoid redundant checks.

Can the time complexity ever reach O(n)?

Yes, in the worst case, when the array is filled with many duplicates, the time complexity can degrade to O(n) as you may need to check every element.

What is the optimal solution for this problem?

The optimal solution uses binary search with careful handling of duplicates. This allows the time complexity to be reduced to O(log n) in many cases, but may still require O(n) in the worst case due to duplicates.

You should reduce the search space when the middle element is greater than the rightmost element. However, when duplicates are present, you may need to carefully compare left, middle, and right elements to ensure you're correctly narrowing down the minimum element.

While it's possible to solve this problem without binary search by using linear scans, binary search is preferred due to its efficiency. A linear scan would result in O(n) time complexity, which is suboptimal compared to the potential O(log n) with binary search.

GhostInterview Solver

Need direct help with Find Minimum in Rotated Sorted Array II instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find Minimum in Rotated Sorted Array II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.