Skip to main content

Find Minimum in Rotated Sorted Array II - Solution & Explanation

HardArrayBinary Search12 min readAsked at: Amazon, Microsoft, Goldman Sachs +2
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [1,3,5]
Output: 1

Example 2:

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

 

Constraints:

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

 

Follow up: This problem is similar to Find Minimum in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?

 

Approach Overview

Problem Overview: You are given a sorted array that has been rotated at an unknown pivot and may contain duplicates. Your task is to return the smallest element in the array. The challenge is handling duplicates, which can break the strict ordering normally used by binary search.

Approach 1: Using Hash Maps for Optimal Time Complexity (O(n) time, O(n) space)

This approach scans the array once while storing visited values in a hash map or set-like structure. As you iterate through the array, track the smallest value seen so far. The hash map ensures constant-time lookups if you want to avoid redundant processing when duplicates appear, though the main work is still a single pass through the array. The key idea is simplicity: since duplicates make pivot detection tricky, a linear scan guarantees the correct minimum regardless of rotation. Prefer this method when constraints are small or when clarity is more important than logarithmic performance.

Approach 2: Using Sorted Arrays and Binary Search (O(log n) average time, O(1) space)

This method exploits the partially sorted structure of the rotated array. Maintain two pointers, left and right, and compute mid each iteration. If nums[mid] < nums[right], the minimum lies in the left half including mid. If nums[mid] > nums[right], the minimum must be in the right half, so move left = mid + 1. Duplicates create ambiguity when nums[mid] == nums[right]; in that case shrink the search space by decrementing right--. This preserves correctness but can degrade to O(n) in worst cases where many duplicates exist. Despite that edge case, the approach typically runs in logarithmic time and uses constant memory.

Recommended for interviews: The binary search solution is what most interviewers expect. It demonstrates understanding of rotated arrays and how duplicates affect search invariants in binary search. A linear scan or hash-based approach proves you can solve the problem correctly, but the optimized pointer-based search shows stronger algorithmic reasoning and space efficiency.

Approach 1: Approach 1: Using Hash Maps for Optimal Time Complexity

This approach utilizes hash maps (dictionaries in Python) to achieve optimal time and space complexity for operations such as insertion, deletion, and lookup. By storing elements as keys in a hash map, we benefit from average-case O(1) time complexity for these operations.

In C, hash maps aren't natively supported, so we implement it using separate chaining. We map keys to a list at each index using a simple modulo-based hash function.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) on average for search, insert, and delete due to the hash map.
Space Complexity: O(n), where n is the number of elements stored.

Try this approach in the editor →

Approach 2: Approach 2: Using Sorted Arrays and Binary Search

This approach leverages sorted arrays to perform efficient binary searches. Operations are optimized for scenarios requiring sorted data, such as when frequent minimum/maximum queries are performed.

In C, we maintain sorted arrays using qsort after each insertion. Searches are performed with a custom binary search function for efficiency.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) for insertion (due to sorting), O(log n) for search (binary search).
Space Complexity: O(n).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Using Hash Maps for Optimal Time Complexity

Time Complexity: O(1) on average for search, insert, and delete due to the hash map.
Space Complexity: O(n), where n is the number of elements stored.

Approach 2: Using Sorted Arrays and Binary Search

Time Complexity: O(n log n) for insertion (due to sorting), O(log n) for search (binary search).
Space Complexity: O(n).

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Hash Map / Linear ScanO(n)O(n)Simple implementation when constraints are small or duplicates make binary search logic unnecessary
Binary Search on Rotated ArrayO(log n) average, O(n) worstO(1)Preferred interview solution for rotated sorted arrays with duplicates

Video Solution

Find Minimum in Rotated Sorted Array II & l | Leetcode 154 & 153 | Hindi • Codebix • 21,204 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Minimum in Rotated Sorted Array II easy or hard?
LeetCode classifies this problem as Hard because duplicates complicate the classic rotated-array binary search logic. Candidates must carefully maintain the search invariant and handle cases where comparisons do not clearly identify the sorted half.
Find Minimum in Rotated Sorted Array II Python/Java solution
Python and Java implementations typically follow the same binary search template: maintain left and right pointers, compute mid, compare nums[mid] with nums[right], and adjust the search range accordingly. Handle duplicates by reducing the right boundary when both values are equal.
How to solve Find Minimum in Rotated Sorted Array II in O(n)?
A straightforward solution iterates through the array and keeps track of the smallest value encountered. This linear scan guarantees correctness even with duplicates and rotations. The algorithm runs in O(n) time and uses O(1) or O(n) space depending on whether auxiliary structures like hash maps are used.
What is the best approach for Find Minimum in Rotated Sorted Array II?
Binary search with two pointers is the most efficient approach. Compare the middle element with the right boundary to decide which half contains the minimum. When duplicates make the comparison ambiguous, shrink the search space by decrementing the right pointer. This gives O(log n) average time and O(1) space.
Is Find Minimum in Rotated Sorted Array II asked at Google/Amazon/Meta?
Rotated array and binary search variations appear frequently in interviews at companies like Amazon, Google, Meta, and Microsoft. This specific problem tests whether candidates can adjust binary search logic when duplicates break strict ordering conditions.
What data structure is used in Find Minimum in Rotated Sorted Array II?
The problem primarily uses arrays combined with binary search techniques. Some simpler solutions may also use hash maps or sets to track visited values, but the optimal approach relies on pointer manipulation within the array itself.
What is the time complexity of Find Minimum in Rotated Sorted Array II?
The optimized binary search solution runs in O(log n) average time and O(1) space. However, when many duplicate values exist, the algorithm may shrink the search range one step at a time, degrading to O(n) in the worst case.

Ready to solve this problem?

Practice Find Minimum in Rotated Sorted Array II with our built-in code editor and test cases.

Practice on FleetCode