Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II is a hard binary search problem solved with the rotated array pattern.
The best approach, optimal (binary search, shrink on ties), runs in O(log n) average, O(n) worst time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from linear scan up.
Problem
A sorted array that may contain duplicates was rotated at an unknown pivot. Return its minimum, doing as little work as possible.
Examples
Example 1
- Input
nums = [3, 3, 1, 3]- Output
1
Example 2
- Input
nums = [2, 2, 2, 0, 1]- Output
0
Constraints
1 <= nums.length <= 5000; duplicates allowed.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 13 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Linear scan | O(n) | O(1) |
| Optimal (binary search, shrink on ties) | O(log n) average, O(n) worst | O(1) |
1Linear scan
O(n)O(1)Return the smallest element. With duplicates, this is also the worst case of any method.
- Track the minimum.
class Solution {
public int findMin(int[] nums) {
int min = nums[0];
for (int x : nums) min = Math.min(min, x);
return min;
}
}class Solution:
def findMin(self, nums: List[int]) -> int:
mn = nums[0]
for x in nums:
mn = min(mn, x)
return mnclass Solution {
public:
int findMin(vector<int>& nums) {
int mn = nums[0];
for (int x : nums) mn = min(mn, x);
return mn;
}
};function findMin(nums) {
let min = nums[0];
for (const x of nums) min = Math.min(min, x);
return min;
}2Optimal (binary search, shrink on ties)
O(log n) average, O(n) worstArrays like [1,1,1,1] force the hi-- step n times.O(1)Same as the distinct version, with one extra case. If nums[mid] == nums[hi], nums[hi] has a copy at mid, so dropping hi keeps the minimum in range: hi--.
- nums[mid] > nums[hi]: lo = mid + 1.
- nums[mid] < nums[hi]: hi = mid.
- Equal: hi--.
class Solution {
public int findMin(int[] nums) {
int lo = 0, hi = nums.length - 1;
while (lo < hi) {
int mid = (lo + hi) >>> 1;
if (nums[mid] > nums[hi]) lo = mid + 1;
else if (nums[mid] < nums[hi]) hi = mid;
else hi--;
}
return nums[lo];
}
}class Solution:
def findMin(self, nums: List[int]) -> int:
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
elif nums[mid] < nums[hi]:
hi = mid
else:
hi -= 1
return nums[lo]class Solution {
public:
int findMin(vector<int>& nums) {
int lo = 0, hi = (int)nums.size() - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) lo = mid + 1;
else if (nums[mid] < nums[hi]) hi = mid;
else hi--;
}
return nums[lo];
}
};function findMin(nums) {
let lo = 0, hi = nums.length - 1;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (nums[mid] > nums[hi]) lo = mid + 1;
else if (nums[mid] < nums[hi]) hi = mid;
else hi--;
}
return nums[lo];
}Edge cases to test
- All values equal
- nums[mid] == nums[hi] with the minimum on either side
Hints
Hint 1
When nums[mid] == nums[hi] you cannot tell which side the drop is on. But you can safely discard hi.
FAQ
What is the best time complexity for Find Minimum in Rotated Sorted Array II?
Optimal (binary search, shrink on ties) runs in O(log n) average, O(n) worst time and O(1) extra space. Arrays like [1,1,1,1] force the hi-- step n times.
Which pattern does Find Minimum in Rotated Sorted Array II use?
It is a binary search problem that uses the rotated array pattern. Other problems with the same pattern: Find How Many Times Array is Rotated, Find Minimum in Rotated Sorted Array, Search in Rotated Sorted Array.
Is there a brute force solution for Find Minimum in Rotated Sorted Array II?
Yes. Linear scan takes O(n) time and O(1) space. Return the smallest element.
Which edge cases should I test for Find Minimum in Rotated Sorted Array II?
All values equal; nums[mid] == nums[hi] with the minimum on either side.