Logo for ammarahmed.ca
Medium
Dec 22, 2025
#binary-search

33. Search in Rotated Sorted Array

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:

1Input: nums = [4,5,6,7,0,1,2], target = 0 2Output: 4

Example 2:

1Input: nums = [4,5,6,7,0,1,2], target = 3 2Output: -1

Example 3:

1Input: nums = [1], target = 0 2Output: -1
  • 1 <= nums.length <= 5000
    • -104 <= nums[i] <= 104
      • All values of nums are unique.
        • nums is an ascending array that is possibly rotated.
          • -104 <= target <= 104

            Constraints:

            Notes

            Intuition

            First find where the array was rotated (the pivot/minimum element), which splits the array into two sorted subarrays. Then determine which subarray contains the target and perform a standard binary search on it.

            Implementation

            First, binary search to find the pivot index using the "minimum in rotated sorted array" approach (where both neighbors are larger, or we're at a boundary). Then check if the target falls in the range from index 0 to pivot-1 (the "left" sorted portion) or from pivot to end (the "right" sorted portion). Binary search the appropriate half.

            Edge-cases

            When determining which half to search, check if target falls within nums[0] to nums[pivot-1]. Handle the case where pivot is 0 (array wasn't rotated or rotated n times).

            • Time: O(log n) — two binary searches
              • Space: O(1) — only tracking pointers

                Complexity

                Solution

                1from typing import List 2 3class Solution: 4 def search(self, nums: List[int], target: int) -> int: 5 n = len(nums) 6 # Find pivot index (min in rotated sorted) 7 l, r = 0, n - 1 8 9 pivot = -1 10 while l <= r: 11 m = (l + r) // 2 12 if (m == 0 or nums[m - 1] > nums[m]) and (m == n - 1 or nums[m + 1] > nums[m]): 13 pivot = m 14 break 15 elif nums[r] < nums[m]: 16 l = m + 1 17 else: 18 r = m - 1 19 20 if pivot == -1: 21 raise RuntimeError("pivot index not found, check your implementation") 22 23 # Search in correct side of array 24 l, r = 0, n - 1 25 if target >= nums[0] and pivot != 0 and target <= nums[pivot - 1]: 26 # search from 0 to pivot 27 r = pivot - 1 28 else: 29 # search from pivot to end 30 l = pivot 31 pass 32 33 while l <= r: 34 m = (l + r) // 2 35 if nums[m] == target: 36 return m 37 elif nums[m] < target: 38 l = m + 1 39 else: 40 r = m - 1 41 42 return -1 43 44 45 46 47if __name__ == "__main__": 48 # Include one-off tests here or debugging logic that can be run by running this file 49 # e.g. print(solution.two_sum([1, 2, 3, 4], 3)) 50 solution = Solution() 51