Search in Rotated Sorted Array II (Duplicates)
Find out whether a value is in a sorted array that was rotated and may contain repeated numbers.
Given an array of integers numstargettruetargetnumsfalse[0, 0, 1, 2, 2, 5, 6][2, 5, 6, 0, 0, 1, 2]
Example 1
Output: true
Example 2
Output: false
The Idea
Imagine a row of number cards laid out from smallest to largest: 0, 0, 1, 2, 2, 5, 6. Someone takes the last three cards, 2, 5 and 6, and puts them at the front without changing their order. The row now reads 2, 5, 6, 0, 0, 1, 2, which is the array in both examples. Is there a 0 in it? Is there a 3?
Reading every card answers both questions, but it ignores the order the cards still have. For a row that is fully sorted, the usual way to search is binary search: look at the middle card and throw away the half that cannot hold the target. That works because everything left of the middle is smaller or equal, and everything right of it is larger or equal. Each look halves the cards left, so a million cards need only about 20 looks.
The rotated row is not sorted as a whole, but it is made of two sorted runs: 2, 5, 6, and then 0, 0, 1, 2. Mark the two ends of the part still being searched with lohimid
When all numbers are different, comparing nums[lo]nums[mid]
In the top row, [1, 0, 1, 1, 1]lomidhi[1, 1, 1, 0, 1]
It does not have to choose. If nums[mid]nums[lo]nums[hi]nums[mid]lohi
Solution Approach
Turning that idea into code means settling three things: how to tell which half is sorted, how to decide which half to keep, and what to do when the three values match.
Telling which half is sorted
How does the code know which half has no drop? In a rotated array, every number in the second run is at most every number in the first run. In Example 1, the second run 0, 0, 1, 2 sits at or below 2, 5, 6. So if nums[mid]nums[lo]nums[mid]
Equal values are the hard case. When nums[lo]nums[mid]nums[hi]midhinums[mid]nums[hi]nums[lo]nums[hi]nums[lo] <= nums[mid]
Choosing the half to keep
Once one half is known to be sorted, can the target be inside it? A sorted left half holds the target only if nums[lo] <= target < nums[mid]nums[mid]himid - 1lomid + 1nums[mid] < target <= nums[hi]
Take target 3 in Example 2. At the start, lohimidnums[mid]nums[lo]hi
When the three values match
What if nums[lo]nums[mid]nums[hi]nums[mid]lohinums[mid][1, 0, 1, 1, 1]lohi
Now nums[lo]nums[mid]nums[hi]hi
Java Solution
The method search
- First, set
to 0 andlo to the last position,hi . Together they mark the part of the array still being searched.nums.length - 1 - Loop while
. In each round, setlo <= hi to the middle position,mid . Written this way, no step goes past the largestlo + (hi - lo) / 2 value, whichint could do on a huge array.lo + hi - If
equalsnums[mid] , returntarget .true - If
,nums[lo] andnums[mid] are all equal, add 1 tonums[hi] , take 1 fromlo , and go on to the next round.hi - Otherwise, if
, the left half is sorted. Keep it withnums[lo] <= nums[mid] when the target is in its range, or move to the right half withhi = mid - 1 .lo = mid + 1 - Otherwise the right half is sorted, and the same check uses
andnums[mid] .nums[hi] - When
passeslo , nothing is left to search, so returnhi .false
The main
public class SearchInRotatedSortedArrayII {
public static boolean search(int[] nums, int target) {
int lo = 0;
int hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) {
return true;
}
if (nums[lo] == nums[mid] && nums[mid] == nums[hi]) {
lo++; // cannot tell which half is sorted:
hi--; // drop both ends and try again
} else if (nums[lo] <= nums[mid]) { // left half is sorted
if (nums[lo] <= target && target < nums[mid]) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} else { // right half is sorted
if (nums[mid] < target && target <= nums[hi]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
}
return false;
}
public static void main(String[] args) {
int[] nums = {2, 5, 6, 0, 0, 1, 2};
System.out.println(search(nums, 0));
System.out.println(search(nums, 3));
System.out.println(search(new int[] {1, 0, 1, 1, 1}, 0));
System.out.println(search(new int[] {1, 1, 1, 0, 1}, 0));
System.out.println(search(new int[] {1, 1, 1}, 2));
System.out.println(search(new int[] {4}, 4));
}
}
Running it prints:
true
false
true
true
false
true
Look at the top two lines: 0 is found in [2, 5, 6, 0, 0, 1, 2]mid
The third and fourth lines, both truelofalse[1, 0, 1, 1, 1]true[1, 1, 1, 0, 1]
The fifth line, false[1, 1, 1]lohi
Complexity
The cost depends on how often a look can throw away half the range, and how often it can only drop the two ends.
- Time: O(log n) when the three values rarely match, O(n) in the worst case. A look that finds a sorted half halves the n numbers left, as plain binary search does. But in an array of many 1s with a single 0, almost every look sees three 1s and removes only two numbers. No method can do better there, because the 0 could be at any position not yet looked at.
- Space: O(1), because the search keeps only
,lo andmid , however many rounds it takes.hi