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.
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 its range. If it does, we search left. If not, we search right. The same logic applies when the right half is sorted. This eliminates half the array at each step, like standard binary search.
nums are unique. Without duplicates, comparing nums[left] with nums[mid] always tells us which half is sorted (a duplicate-laden array can make that comparison ambiguous).Walk through the array and check every element. If we find the target, return its index. If we reach the end, return -1.
This ignores the sorted and rotated structure entirely, so it works on any array, not only rotated sorted ones. It runs in O(n) and violates the problem's O(log n) requirement, but it establishes a baseline for the optimization that follows.
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 is not fully sorted, but splitting it at any midpoint always leaves at least one half sorted. The single point where the order breaks (the rotation point) can lie in only one of the two halves, so the other half is a clean ascending run.
At each step, determine which half is sorted, then decide where the target could be. Because a sorted half is bounded by its two endpoints, a range check on those endpoints tells us whether the target can be inside it. If it can, search that half. If it cannot, the target must be in the other half. Either way, half the array is discarded.
To 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.The nums[left] <= nums[mid] comparison is reliable because values are unique. In an ascending run, the leftmost value is at most the midpoint value, so when nums[left] <= nums[mid] holds, the segment [left...mid] contains no rotation point and is sorted. When it fails, nums[left] > nums[mid] means the rotation point sits somewhere in [left...mid], which forces [mid...right] to be a single ascending run. Exactly one half can hold the rotation point, so identifying the sorted half and range-checking the target against its endpoints never discards the half that contains the answer.
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.