Search in Rotated Sorted Array
Pattern: Binary search on rotated arrays
One of the two halves around mid is always sorted. Find which by comparing nums[lo] with nums[mid], check whether the target lies inside that sorted half, and keep the half that can hold it.
The problem
An array of different whole numbers was sorted in increasing order and then rotated at an unknown point, so
[0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. Given the rotated array and a target, return the target's
index or -1, in O(log n) time. For example, [4, 5, 6, 7, 0, 1, 2] gives 4 for target 0 and -1 for target 3.
The function name search matches LeetCode. Use the link above for the exact statement and limits.
Think first
- How many places in the array have a value bigger than the next one?
- Splitting at
mid, can both halves contain that place? - For a sorted half, how do you test in O(1) whether the target could be inside it?
Hints
Brute force, and why it fails
Scan the array: O(n). Or find the rotation point first (Find Minimum) and then binary search the correct sorted run. That is O(log n) with two searches. The one-pass version below does it in a single search.
The key insight
You cannot ask "is the target left or right of mid" directly, but you can always find one sorted half. Inside a sorted half, the target either clearly fits or clearly does not, and that decides which half to keep.
Dry run
nums = [4, 5, 6, 7, 0, 1, 2] and target = 0. The array rises, falls once after 7, and rises again.
The plan
lo = 0, hi = n - 1
while lo <= hi:
mid = lo + (hi - lo) / 2
if nums[mid] == target: return mid
if nums[lo] <= nums[mid]:
if nums[lo] <= target < nums[mid]: hi = mid - 1
else: lo = mid + 1
else:
if nums[mid] < target <= nums[hi]: lo = mid + 1
else: hi = mid - 1
return -1
Complexity
- Time
O(log n)- Space
O(1)
Each step throws away half of the range.
Edge cases
- Not rotated at all: the left half is always sorted, and this is plain binary search.
- One value or two values, such as
[3, 1]with target 1:loequalsmid, and≤keeps the left-half test correct. - Target missing: the range empties and the answer is -1.
I've built my logic, show the code
Java
class Solution {
public 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 lo..mid is sorted
if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
} else { // right half mid..hi is sorted
if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
}C++
class Solution {
public:
int search(vector<int>& nums, int target) {
int lo = 0, hi = static_cast<int>(nums.size()) - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) return mid;
if (nums[lo] <= nums[mid]) { // left half lo..mid is sorted
if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
} else { // right half mid..hi is sorted
if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
};Python
class Solution:
def search(self, nums: list[int], target: int) -> int:
lo, hi = 0, len(nums) - 1
while lo <= hi:
mid = (lo + hi) // 2
if nums[mid] == target:
return mid
if nums[lo] <= nums[mid]: # left half lo..mid is sorted
if nums[lo] <= target < nums[mid]:
hi = mid - 1
else:
lo = mid + 1
else: # right half mid..hi is sorted
if nums[mid] < target <= nums[hi]:
lo = mid + 1
else:
hi = mid - 1
return -1Common mistakes
- Writing
nums[lo] < nums[mid], which breaks whenlo == mid. - Checking only one end of the sorted half.
- Comparing the target with
nums[mid]alone, as in plain binary search, which picks the wrong half.