An array originally sorted in ascending order was rotated at some pivot. Given the rotated array nums (all elements unique) and a target, return the index of target or -1. You must achieve O(log n) runtime.
Example
Input: nums = [4,5,6,7,0,1,2], target = 0Output:4// Originally [0,1,2,4,5,6,7], rotated at index 4
Constraints
โข 1 โค nums.length โค 5000โข -10โด โค nums[i] โค 10โดโข All values uniqueโข Must run in O(log n)
02
Section Two ยท Approach 1
Linear Scan โ O(n)
Walk through the array. Correct but violates the O(log n) requirement.
Problem: O(n) ignores the partial sorted structure. In a rotated sorted array, at least one half (left or right of mid) is always sorted โ binary search on that half.
Metric
Value
Time
O(n)
Space
O(1)
03
Section Three ยท Approach 2
Modified Binary Search โ O(log n)
At each step, determine which half is sorted (compare nums[left] to nums[mid]). If target lies in the sorted half's range, search there. Otherwise, search the other half.
๐ก Mental model: Imagine a clock with unevenly spaced numbers. At any point you look (mid), you can tell if the left half or right half goes in order. If your target number fits within the ordered half, search there. If not, go to the "chaotic" half โ which will have its own ordered sub-section when you halve again.
classSolution {
publicintsearch(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[left] <= nums[mid]) { // left half sortedif (nums[left] <= target && target < nums[mid])
right = mid - 1;
else
left = mid + 1;
} else { // right half sortedif (nums[mid] < target && target <= nums[right])
left = mid + 1;
else
right = mid - 1;
}
}
return -1;
}
}
Python โ Modified Binary Search
classSolution:
defsearch(self, nums: list[int], target: int) -> int:
left, right = 0, len(nums) - 1while left <= right:
mid = (left + right) // 2if nums[mid] == target: return mid
if nums[left] <= nums[mid]: # left half sortedif nums[left] <= target < nums[mid]:
right = mid - 1else:
left = mid + 1else: # right half sortedif nums[mid] < target <= nums[right]:
left = mid + 1else:
right = mid - 1return -1
06
Section Six ยท Analysis
Complexity Analysis
Approach
Time
Space
Trade-off
Linear Scan
O(n)
O(1)
Ignores structure
Modified Binary Search โ optimal
O(log n)
O(1)
One sorted half always identifiable
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
Case
Input
Why It Matters
No rotation
[1,2,3,4,5]
Standard sorted array. Left half always sorted โ works normally.
Rotated by 1
[5,1,2,3,4]
Pivot at index 0. Right half sorted for most steps.
Single element
[3], target=3
mid=0, found immediately.
Target not found
[4,5,6,7,0,1,2], target=3
Search space exhausted โ return -1.
Two elements
[3,1], target=1
mid=0, nums[0]=3โคnums[0] left sorted, target not in [3,3] โ go right.
โ Common Mistake: Using < instead of โค in nums[left] โค nums[mid]. When left == mid (two-element subarray), the left "half" is a single element and is sorted. Without โค, you'd incorrectly assume the right half is sorted and make wrong decisions.