Introduction
LeetCode 154 – Find Minimum in Rotated Sorted Array II is a classic binary search interview problem.
This problem is an advanced version of:
- Find Minimum in Rotated Sorted Array
The major difference is:
The array may contain duplicates.
That small change makes the problem much harder because duplicates can break normal binary search logic.
This problem teaches:
- Modified binary search
- Rotated array concepts
- Handling duplicates
- Edge case optimization
- Interview problem-solving techniques
Problem Link
🔗 Problem
LeetCode 154: Find Minimum in Rotated Sorted Array II
Official Problem:
Problem Statement
An ascending sorted array is rotated between 1 and n times.
Example:
may become:
or
Given the rotated sorted array nums that may contain duplicates, return the minimum element.
Example
Example 1
Input:
Output:
Example 2
Input:
Output:
Understanding Rotated Sorted Arrays
Normally a sorted array looks like:
After rotation:
The minimum element becomes the pivot point.
Our goal is to efficiently find this pivot.
Brute Force Approach
Intuition
Traverse the entire array and keep track of the smallest element.
Algorithm
- Initialize minimum as first element.
- Traverse the array.
- Update minimum whenever a smaller element appears.
- Return minimum.
Java Code – Brute Force
Dry Run – Brute Force
Input:
Traversal:
| Element | Minimum |
| 2 | 2 |
| 2 | 2 |
| 2 | 2 |
| 0 | 0 |
| 1 | 0 |
Final answer:
Complexity Analysis – Brute Force
Time Complexity
Space Complexity
Can We Do Better?
Yes.
Since the array is sorted and rotated, we can use Binary Search.
However, duplicates make the problem tricky.
Binary Search Intuition
In a rotated sorted array:
- One half is always sorted.
- The minimum lies in the unsorted half.
Without duplicates, binary search becomes straightforward.
But duplicates create ambiguity.
Example:
If:
we cannot determine which side contains the minimum.
So we shrink the search space carefully.
Key Binary Search Observations
Case 1
If:
nums[mid]<nums[right]nums[mid] < nums[right]nums[mid]<nums[right]
then the right half is sorted, and minimum may lie at mid or left side.
Move:
Case 2
If:
nums[mid]>nums[right]nums[mid] > nums[right]nums[mid]>nums[right]
then minimum lies in the right half.
Move:
Case 3
If:
nums[mid]=nums[right]nums[mid] = nums[right]nums[mid]=nums[right]
we cannot identify the correct side.
Safely reduce search space:
Optimized Binary Search Approach
Steps
- Initialize two pointers:
leftright- Find middle element.
- Compare
nums[mid]withnums[right]. - Narrow the search space accordingly.
- Continue until pointers meet.
Java Binary Search Solution
Dry Run – Binary Search
Input
Initial State
| Left | Right |
| 0 | 4 |
Iteration 1
Mid
Value:
Since:
2>12 > 12>1
Move:
Now:
| Left | Right |
| 3 | 4 |
Iteration 2
Mid
Value:
Since:
0<10 < 10<1
Move:
Now:
| Left | Right |
| 3 | 3 |
Final Answer
Time Complexity Analysis
Average Case
Worst Case
Due to duplicates:
Why Worst Case Becomes O(N)
Consider:
Every comparison becomes:
We can only shrink by one element:
This degrades binary search to linear complexity.
Interview Explanation
In interviews, explain:
Duplicates destroy the ability to always determine the sorted half uniquely. When nums[mid] == nums[right], we cannot confidently eliminate one side, so we reduce the search space by one element.This is the key insight interviewers look for.
Common Mistakes
1. Using Standard Binary Search Logic
Standard rotated-array logic fails with duplicates.
2. Ignoring Duplicate Case
This condition is essential:
3. Infinite Loop Errors
Always update pointers carefully.
Alternative Simpler Binary Search
A cleaner version:
This is the most common interview solution.
FAQs
Q1. Why does binary search become O(N)?
Duplicates prevent us from discarding half the search space confidently.
Q2. Why compare with nums[right]?
It helps identify whether the minimum lies on the left or right side.
Q3. Is this problem important for interviews?
Yes.
It is a very popular advanced binary search interview problem.
Conclusion
LeetCode 154 is an excellent problem for mastering:
- Modified binary search
- Rotated sorted arrays
- Duplicate handling
- Search optimization
The key challenge is handling:
nums[mid]=nums[right]nums[mid] = nums[right]nums[mid]=nums[right]
correctly.
Once you understand this pattern, many advanced binary search interview problems become much easier.





