Search in Rotated Sorted Array
Search in Rotated Sorted Array is LeetCode 33 (Medium). An array of distinct integers was sorted in ascending order and then possibly rotated: cut at some index, with the two pieces swapped. Given target, return its index, or −1 if it is not there.
- The solution must run in O(log n) time.
- A rotation by 0 is allowed, so the array may simply be sorted.
- 1 <= nums.length <= 5000
- -10⁴ <= nums[i] <= 10⁴
- All values of nums are unique.
- nums is an ascending array that is possibly rotated.
- -10⁴ <= target <= 10⁴
Intuition
A rotated array has exactly one drop, where a big value is followed by a small one. Split the range at the middle and the drop can only be on one side, so at least one half is fully sorted.
A sorted half is easy to question: target is in it exactly when it lies between that half's first and last values. So each step does two things:
- Find the sorted half by comparing the left end with the middle.
- Ask whether the target fits inside it. If it does, search there; if not, it can only be in the other half.
Either way half the range is gone.
A sorted array that was rotated, with a request for a value's index in O(log n): find the sorted side of the middle, then range-check the target against it.
Approach
Before reading on, split a rotated array at its middle and mark which side has no drop. How do you tell that side from the values at the left end and the middle alone? Then build an O(log n) search on that test.
Check the middle first
With lo = 0, hi = n - 1 and mid between them, return mid if nums[mid] == target. The range tests below then only need to decide where the target is not.
Left half sorted
If nums[lo] <= nums[mid], the values from lo to mid rise with no drop. When nums[lo] <= target < nums[mid] the target can only be among them, so set hi = mid - 1; otherwise set lo = mid + 1.
Right half sorted
Otherwise the drop sits between lo and mid, so the values from mid to hi rise. When nums[mid] < target <= nums[hi], set lo = mid + 1; otherwise set hi = mid - 1.
An empty range means absent
Loop while lo <= hi. Every pass removes the middle and a half that cannot hold the target, so once the range is empty, return −1.
Code in Python, C++ and Java
Common pitfalls
A strict comparison in the sorted-half test
if nums[lo] < nums[mid]:
if nums[lo] <= nums[mid]:
With two values left, mid equals lo, and that one-value left half counts as sorted. The strict test calls the right half sorted instead. On [3, 1] with target 1, it asks whether 1 lies above 3 and at most 1, which is impossible, so it drops the 1 and returns −1.
Leaving the left end out of the range test
if nums[lo] < target < nums[mid]:
if nums[lo] <= target < nums[mid]:
The first value of the sorted half can be the target. On [4, 5, 6, 7, 0, 1, 2] with target 4, the left half 4 to 7 is sorted, but 4 < 4 fails, so the search moves right, away from index 0, and returns −1.
Edge cases
[1, 3, 5, 7]The left end is never bigger than the middle, so the left half always counts as sorted and the loop behaves exactly like a plain binary search.
Complexity
lo, hi and mid are stored.Search in Rotated Sorted Array FAQ
Can I find the rotation point first and then do a normal binary search?
Yes. Locate the smallest value as in Find Minimum in Rotated Sorted Array, then binary search whichever of the two sorted pieces could hold the target. Both loops are O(log n); the one-pass version just avoids writing two.
What breaks if the array can contain repeated values?
When the left end and the middle hold the same value, that comparison no longer shows which half is sorted, and the search has to fall back to shrinking the range one value at a time. That version is Search in Rotated Sorted Array II, which only asks whether the target exists.