Find Minimum in Rotated Sorted Array II (Duplicates)
Find the smallest value in a sorted array that was rotated and may hold the same value more than once.
An array of integers was sorted in ascending order and then rotated: some of its first values, possibly none, were moved to its end in the same order. The array may hold the same value more than once. Given the rotated array nums
Example 1
Output: 1
Example 2
Output: 0
The Idea
Picture a row of number cards laid out in order: 0, 1, 2, 2, 2. Someone lifts the first two cards off the front and puts them at the back, so the row now reads 2, 2, 2, 0, 1. That is Example 2. The card we want is the 0, and it sits right where the order drops.
Reading the whole row and keeping the smallest card always works, but it looks at every card. A sorted array is usually searched faster by halving: look at the middle, decide which half holds the answer, and throw the other half away. Searching by halving like that is called binary search. Can a rotated row still be halved?
Keep two markers on the part of the array still in play: lohimidhi
In Example 2, midhimidmidhimid
Repeated values raise a new question: what if the middle value and the last value are equal? Look at the picture below.
In Row A, midhimidmid
So we cannot throw away half of the range. We can still throw away one value: the one at himid
Solution Approach
Turning that idea into code means settling three things: what to compare the middle value with, how far to move a marker in each case, and what the equal case costs.
Comparing the middle with the end
Why compare with the last value and not the first? Take [1, 3, 5][3, 5, 1]
Moving the markers
How far can each case move a marker? When nums[mid] > nums[hi]midlomid + 1nums[mid] < nums[hi]midhimid
When the two are equal, hilohi
What the equal case costs
The equal case removes one value, not half the range. With all values different it never happens, and a sorted array of 1,000 values takes 10 passes. With many repeats it can be as slow as reading every value: for 1,000 values that are all 3 except a 1 at index 0, the loop runs 999 times.
No search can do better on such input. In an array of 3s with one 1 hidden in it, any position not yet looked at could be the 1.
Java Solution
The method findMin
- First, set
to 0 andlo to the last index,hi , so the whole array is in play.nums.length - 1 - Loop while
. Inside, find the middle positionlo < hi withmid , which rounds down, solo + (hi - lo) / 2 is always left ofmid .hi - If
, setnums[mid] > nums[hi] . Iflo = mid + 1 , setnums[mid] < nums[hi] .hi = mid - Otherwise the two values are equal, so drop the last value with
.hi-- - When
andlo meet, returnhi .nums[lo]
The main[3, 3, 1, 3]
import java.util.Arrays;
public class FindMinimumInRotatedSortedArrayII {
public static int findMin(int[] nums) {
int lo = 0;
int hi = nums.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) {
lo = mid + 1; // minimum is to the right of mid
} else if (nums[mid] < nums[hi]) {
hi = mid; // minimum is mid or to its left
} else {
hi--; // equal: side unknown, but nums[hi] has a copy at mid
}
}
return nums[lo];
}
public static void main(String[] args) {
int[][] inputs = {
{1, 3, 5},
{2, 2, 2, 0, 1},
{3, 3, 3, 1, 3},
{3, 1, 3, 3, 3},
{3, 3, 1, 3},
{1, 1, 1},
{7}
};
for (int[] nums : inputs) {
System.out.println(Arrays.toString(nums) + " -> " + findMin(nums));
}
}
}
Running it prints:
[1, 3, 5] -> 1
[2, 2, 2, 0, 1] -> 0
[3, 3, 3, 1, 3] -> 1
[3, 1, 3, 3, 3] -> 1
[3, 3, 1, 3] -> 1
[1, 1, 1] -> 1
[7] -> 7
Look at the lines for [3, 3, 3, 1, 3][3, 1, 3, 3, 3]midhi
The line for [3, 3, 1, 3]midhihilohi = mid
For [1, 1, 1]hilo[7]
Complexity
The work is the number of passes of the loop, and each pass does one comparison and moves one marker.
- Time: O(log n) when values are distinct, O(n) in the worst case. The bigger and smaller cases halve the range on each pass, but the equal case removes only one value, so an array of 3s with a 1 at index 0 takes n - 1 passes.
- Space: O(1). However many passes the loop takes, it only updates
,lo andhi .mid
When no value repeats, the equal case cannot happen and the search is always O(log n): see