Search in Rotated Sorted Array
The problem
Section titled “The problem”A sorted array of distinct integers was rotated at some unknown position, so
[0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. Given the rotated array
and a target, return the target’s index, or -1. It must run in O(log n).
nums = [4, 5, 6, 7, 0, 1, 2], target = 0 -> 4nums = [4, 5, 6, 7, 0, 1, 2], target = 3 -> -1Key insight
Section titled “Key insight”The whole array is not sorted, so you cannot just compare the middle to the target and
pick a side. But the rotation point lies in only one of the two halves, so at least
one half around mid is always fully sorted.
At each step, find the sorted half. If nums[lo] <= nums[mid], the left half
[lo, mid] is sorted; otherwise the right half [mid, hi] is. Then check whether the
target lies inside that sorted half’s value range. If so, search it; if not, the target
can only be in the other half. Either way you discard half the array, in one pass.
Show solutionHide solution
def search(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) - 1 while lo <= hi: mid = lo + (hi - lo) // 2 if nums[mid] == target: return mid if nums[lo] <= nums[mid]: # left half [lo, mid] is sorted if nums[lo] <= target < nums[mid]: hi = mid - 1 else: lo = mid + 1 else: # right half [mid, hi] is sorted if nums[mid] < target <= nums[hi]: lo = mid + 1 else: hi = mid - 1 return -1export function search(nums: number[], target: number): number { let lo = 0; let hi = nums.length - 1; while (lo <= hi) { const mid = lo + Math.floor((hi - lo) / 2); if (nums[mid] === target) return mid; if (nums[lo] <= nums[mid]) { // left half [lo, mid] is sorted if (nums[lo] <= target && target < nums[mid]) hi = mid - 1; else lo = mid + 1; } else { // right half [mid, hi] is sorted if (nums[mid] < target && target <= nums[hi]) lo = mid + 1; else hi = mid - 1; } } return -1;}class Solution { public int search(int[] nums, int target) { int lo = 0, hi = nums.length - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] == target) return mid; if (nums[lo] <= nums[mid]) { // left half [lo, mid] is sorted if (nums[lo] <= target && target < nums[mid]) hi = mid - 1; else lo = mid + 1; } else { // right half [mid, hi] is sorted if (nums[mid] < target && target <= nums[hi]) lo = mid + 1; else hi = mid - 1; } } return -1; }}Complexity
Section titled “Complexity”- Time: O(log n) — each iteration discards half of the range; no pivot-finding pre-pass is needed.
- Space: O(1) — only
lo,hiandmid.
Watch out for
Section titled “Watch out for”<=innums[lo] <= nums[mid]. Whenlo == mid(a one- or two-element range), the left “half” is a single element and counts as sorted. Using<sends[3, 1]with target 1 down the wrong branch.- Strict on the
midside of the range check. You already testednums[mid], so usetarget < nums[mid](left) andnums[mid] < target(right). The other end of the range is inclusive. - Distinct values only. Duplicates break the “which half is sorted” test (see Variations).
Variations
Section titled “Variations”Find Minimum in Rotated Sorted Array (LeetCode 153)
Section titled “Find Minimum in Rotated Sorted Array (LeetCode 153)”Return the smallest element of a rotated sorted array of distinct values. The trick:
compare nums[mid] with nums[hi]. If nums[mid] > nums[hi], the minimum is to the
right (lo = mid + 1); otherwise it is at mid or left (hi = mid). Stop when
lo == hi.
Search in Rotated Sorted Array II (LeetCode 81)
Section titled “Search in Rotated Sorted Array II (LeetCode 81)”Same problem, but values may repeat, and you only return whether the target exists.
The trick: when nums[lo] == nums[mid] == nums[hi] you cannot tell which half is
sorted, so shrink both ends by one. That makes the worst case O(n), for example on
all-equal arrays.
Find First and Last Position of Element in Sorted Array (LeetCode 34)
Section titled “Find First and Last Position of Element in Sorted Array (LeetCode 34)”Return the first and last index of the target in a sorted array with duplicates.
The trick: run the “first index with nums[i] >= x” binary search twice, for
target and for target + 1. The range is [first, second - 1].