Classic binary search adapted for a rotated array. The invariant: one half is always fully sorted — use that to decide where the target must be.
Given an integer array nums sorted in ascending order (with distinct values) that has been rotated at an unknown pivot, and an integer target, return the index of target or -1 if not present. Must run in O(log n).
A rotated sorted array is two sorted segments glued together. At any mid, either the left half or the right half is guaranteed to be fully sorted. Whichever is sorted, we can check in O(1) whether the target lies inside it.
Check which half is sorted. Check if the target belongs in it. If yes — search there. If no — search the other half. Repeat.
nums[left]=4 <= nums[mid]=7 → left half [4..7] is sorted. Is target 0 in [4,7]? No → left = mid+1 = 4.
nums[left]=0 <= nums[mid]=1 → left half [0,1] is sorted. Is target 0 in [0,1]? Yes → right = mid-1 = 4.
nums[4] == target → return 4. Found!
class Solution { public int search(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 is sorted if (target >= nums[left] && target < nums[mid]) right = mid - 1; // target in left half else left = mid + 1; // target in right half } else { // right half is sorted if (target > nums[mid] && target <= nums[right]) left = mid + 1; // target in right half else right = mid - 1; // target in left half } } return -1; } }
class Solution: def search(self, nums: list[int], target: int) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: # left half sorted if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: # right half sorted if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1
vs LC 153: LC 153 finds the pivot using First True. LC 33 doesn't need the pivot — it uses the sorted-half invariant directly to search for the target in one pass.