Last Updated: March 29, 2026
We have a sorted array that's been rotated at some unknown pivot. For example, [0, 1, 2, 4, 5, 6, 7] rotated at index 3 becomes [4, 5, 6, 7, 0, 1, 2]. The rotation breaks the global sorted order, but it creates two sorted halves: [4, 5, 6, 7] and [0, 1, 2]. We need to find a given target in this array, and we need to do it in O(log n) time.
The key observation is that even after rotation, at least one half of the array (when split at the midpoint) is always sorted. If the left half is sorted, we can check whether the target falls within that range. If it does, we search left. If not, we search right. The same logic applies when the right half is sorted. This lets us eliminate half the array at each step, just like standard binary search.
1 <= nums.length <= 5000 → With n up to 5000, even O(n) linear scan would be fast. But the problem explicitly requires O(log n), which means binary search.nums are unique → No duplicates simplifies the binary search logic. We can reliably determine which half is sorted by comparing nums[left] with nums[mid].-10^4 <= target <= 10^4 → Standard integer range, no overflow concerns.The most direct approach: just walk through the array and check every element. If we find the target, return its index. If we reach the end, return -1.
This completely ignores the sorted/rotated structure, which means it works on any array, not just rotated sorted ones. It's O(n), so it violates the problem's O(log n) requirement, but it's a clean baseline that makes it clear what we're trying to improve.
i from 0 to n - 1.nums[i] == target, return i.-1.The linear scan works but doesn't meet the O(log n) requirement. Since the array has two sorted halves, we can use a modified binary search to eliminate half the search space at each step.
Standard binary search works on fully sorted arrays. A rotated sorted array isn't fully sorted, but here's the crucial insight: when you split it at any midpoint, at least one half is always sorted.
So the algorithm is: at each step, figure out which half is sorted. Then check if the target could be in that sorted half (by comparing target to the half's endpoints). If yes, search that half. If no, search the other half.
How do we determine which half is sorted? Compare nums[left] with nums[mid]:
nums[left] <= nums[mid], the left half [left...mid] is sorted.[mid...right] is sorted.This works because the rotation point (the only place where order breaks) can be in at most one half.
The algorithm exploits a fundamental property of rotated sorted arrays: splitting at any point always produces at least one sorted half. In a sorted half, we can use simple range checks (target >= min && target <= max) to determine if the target could be there. If it can, we narrow our search to that half. If it can't, the target must be in the other half. Either way, we eliminate half the array in each iteration.
The nums[left] <= nums[mid] check is the key decision point. In a non-rotated portion, the leftmost value is always less than or equal to the midpoint. If this condition fails, the rotation point lies in the left half, which means the right half must be perfectly sorted.
left = 0 and right = nums.length - 1.left <= right:mid = left + (right - left) / 2.nums[mid] == target, return mid.nums[left] <= nums[mid]):[nums[left], nums[mid]), search left: right = mid - 1.left = mid + 1.(nums[mid], nums[right]], search right: left = mid + 1.right = mid - 1.-1.