Problem
Given an array of distinct values that was sorted in ascending order and then rotated, return the index of a target, or -1 if it is not present, in O(log n) time.
Original: LeetCode 33: Search in Rotated Sorted Array.
Examples
Example 1
`nums = [4, 5, 6, 7, 0, 1, 2], target = 0``4`Example 2
`nums = [4, 5, 6, 7, 0, 1, 2], target = 3``-1`Example 3
`nums = [1], target = 0``-1`Example 4
`nums = [1], target = 1``0`Constraints
1 <= nums.length <= 5000-10^4 <= nums[i] <= 10^4- All values of
numsare unique. numsis an ascending array that is possibly rotated.-10^4 <= target <= 10^4
Hints
Hint 1
A single mid can no longer be compared with the target in isolation, because the drop may sit on either side. Distinct values mean nums[lo] <= nums[mid] is a reliable test that the left half is sorted. The official problem forbids a linear scan.
Hint 2
Boundary binary search on a rotated range: identify the sorted half, then decide whether the target is inside it.
Solution
Tried it yourself? The approach, complexity, and code stay hidden until you choose.
Approach
Brute force. A linear scan: O(n).
Optimal. While lo <= hi, mid = lo + (hi - lo) / 2, and return mid on an exact hit. If nums[lo] <= nums[mid], the left half is sorted: if the target is in [nums[lo], nums[mid]), search left, otherwise right. If the left half is not sorted, the right half is, and you do the symmetric test against (nums[mid], nums[hi]].
Alternative. First find the minimum, then run ordinary binary search in the correct half. That is two logarithmic passes and is easier to explain; the one-pass version above is what interviewers often hope to see.
Complexity
- Time
- O(log n)
O(log n) time, O(1) extra space.
- Space
- O(1)
Code
public class SearchInRotatedSortedArray {
static int search(int[] nums, int target) {
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) return mid;
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 -1;
}
public static void main(String[] args) {
System.out.println(search(new int[] {4, 5, 6, 7, 0, 1, 2}, 0)); // 4
System.out.println(search(new int[] {4, 5, 6, 7, 0, 1, 2}, 3)); // -1
System.out.println(search(new int[] {1}, 0)); // -1
System.out.println(search(new int[] {1}, 1)); // 0
}
}Edge cases
- Official samples: [4,5,6,7,0,1,2] with target 0 returns 4; with target 3 returns -1.
- A single element, as in [1] with 0 or 1.
- The array is not actually rotated: the left-half-is-sorted branch handles it.
Follow-ups
- With duplicates, equal values at lo, mid and hi can force you to shrink the range by one, and the worst case becomes linear.
- Two numbers that sum to k in a rotated sorted array is already answered on Kuchup's Zalando technical-round page, so it is not repeated here.
Interview script
"At mid, one half is still sorted. I test whether the target sits inside that sorted half; if it does I search there, otherwise I search the rotated half. I still compute mid so the index sum cannot overflow. Logarithmic time."
Source: LeetCode 33: Search in Rotated Sorted Array
Binary search on the answer
Sources (1)
- LeetCode 33: Search in Rotated Sorted Arrayleetcode.com