Problem
Section titled “Problem”There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly left rotated at an unknown index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be left rotated by 3 indices and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0Output: 4Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3Output: -1Example 3:
Input: nums = [1], target = 0Output: -1Constraints:
-
nums.length -
nums[i] - All values of
numsare unique. numsis an ascending array that is possibly rotated.-
target
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”A rotated sorted array is two ascending runs, [4,5,6,7] and [0,1,2], where every value of the first run is larger than every value of the second. Plain binary search fails because the whole range is not sorted.
But cut any range [lo, hi] at mid, and at least one of the two halves is sorted — the rotation point can lie in only one of them. Which one is easy to tell with distinct values: if nums[lo] <= nums[mid], the left half [lo, mid] contains no drop and is sorted; otherwise the drop is on the left, so the right half [mid, hi] is sorted.
For a sorted half, a range check tells us immediately whether target can be in it (nums[lo] <= target < nums[mid] for the left half). If it can, keep that half; if not, the target can only be in the other half. Either way half the range is discarded per step, so the search stays .
Algorithm
Section titled “Algorithm”lo = 0,hi = n - 1.- While
lo <= hi:mid = lo + (hi - lo) / 2. Ifnums[mid] == target, returnmid.- If
nums[lo] <= nums[mid](left half sorted): ifnums[lo] <= target && target < nums[mid], sethi = mid - 1, elselo = mid + 1. - Otherwise (right half sorted): if
nums[mid] < target && target <= nums[hi], setlo = mid + 1, elsehi = mid - 1.
- Return
-1.
Walkthrough
Section titled “Walkthrough”Example 1, target = 0. Blue is mid, purple is the half that is sorted, grey is discarded:
Complexity
Section titled “Complexity”- Time: — each iteration halves
[lo, hi]. - Space: .
Edge cases
Section titled “Edge cases”- Not rotated at all:
nums[lo] <= nums[mid]always holds and the algorithm is ordinary binary search. lo == mid(a range of two elements):nums[lo] <= nums[mid]is true, the left “half” is the single elementnums[mid]already checked, so the range test fails and the search moves right — correct.- Single element (Example 3): checked once, then the range closes and
-1is returned.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int search(vector<int>& nums, int target) { int lo = 0, hi = (int)nums.size() - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] == target) return mid; if (nums[lo] <= nums[mid]) { // [lo, mid] is sorted if (nums[lo] <= target && target < nums[mid]) hi = mid - 1; else lo = mid + 1; } else { // [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]) { // [lo, mid] is sorted if (nums[lo] <= target && target < nums[mid]) hi = mid - 1; else lo = mid + 1; } else { // [mid, hi] is sorted if (nums[mid] < target && target <= nums[hi]) lo = mid + 1; else hi = mid - 1; } } return -1; }}class Solution: def search(self, nums: list[int], target: int) -> int: lo, hi = 0, len(nums) - 1 while lo <= hi: mid = (lo + hi) // 2 if nums[mid] == target: return mid if nums[lo] <= nums[mid]: # [lo, mid] is sorted if nums[lo] <= target < nums[mid]: hi = mid - 1 else: lo = mid + 1 else: # [mid, hi] is sorted if nums[mid] < target <= nums[hi]: lo = mid + 1 else: hi = mid - 1 return -1Give it a try first
Attempt the problem on LeetCode — the code will still be here.
![Rotated binary search for 0 in [4,5,6,7,0,1,2], step 1 of 4](/CodePrime/img/codes/search-in-rotated-sorted-array/rotated-search/01.webp)
![Rotated binary search for 0 in [4,5,6,7,0,1,2], step 2 of 4](/CodePrime/img/codes/search-in-rotated-sorted-array/rotated-search/02.webp)
![Rotated binary search for 0 in [4,5,6,7,0,1,2], step 3 of 4](/CodePrime/img/codes/search-in-rotated-sorted-array/rotated-search/03.webp)
![Rotated binary search for 0 in [4,5,6,7,0,1,2], step 4 of 4](/CodePrime/img/codes/search-in-rotated-sorted-array/rotated-search/04.webp)