Search in Rotated Sorted Array

medium

You are given an integer array nums that was originally sorted in ascending order with distinct values. Before it reached you, it may have been left-rotated at an unknown pivot index k (1 <= k < nums.length), moving the first k elements to the back. For example, [0,1,2,4,5,6,7] rotated at pivot 3 becomes [4,5,6,7,0,1,2].

Given the possibly rotated array nums and an integer target, return the index of target in nums, or -1 if it is not present.

Your algorithm must run in O(log n) time.

Hints

A rotated sorted array is not chaos — it is two sorted runs glued together. What does that tell you about any window you look at?
Pick any mid and compare nums[lo] with nums[mid]. One of the two halves is guaranteed to be perfectly sorted — which one, and how do you know?
A sorted half can answer whether the target lies inside it using just its two endpoint values. If yes, search it; if no, the target must be in the other half. That is classic binary search with one extra check per step.

Common doubts

The rotation creates exactly one break point, and it can sit in only one half of the window. If nums[lo] <= nums[mid], there is no break between lo and mid, so the left half is sorted; otherwise the break is on the left, which forces the right half [mid..hi] to be sorted.
Yes. With no rotation the left half is always sorted, so the algorithm gracefully degenerates into classic binary search.
The checks are nums[lo] <= target < nums[mid] and nums[mid] < target <= nums[hi]. The value nums[mid] was already compared against target, so it is excluded — but nums[lo] and nums[hi] have not been examined yet and must stay inclusive.

Interview follow-ups

When nums[lo] == nums[mid] == nums[hi] you cannot tell which half is sorted — shrink both ends by one and continue. Correctness survives, but the worst case degrades to O(n). That is the problem Search in Rotated Sorted Array II.
Same sorted-half reasoning, but compare nums[mid] against nums[hi]: if nums[mid] > nums[hi] the minimum lies right of mid, otherwise it is at mid or to its left. That is Find Minimum in Rotated Sorted Array.
Yes — find the rotation point in O(log n), decide which sorted run holds the target, then binary-search that run. Two passes instead of one, still O(log n) overall; interviewers usually prefer the one-pass version but accept both.

Fun facts

  • A rotated sorted array is exactly what a circular buffer looks like when its start pointer has moved — the same search idea applies to wrapped logs and ring buffers in real systems.
  • The sorted-half interrogation is a family trick: with one small twist each, it also cracks Find Minimum in Rotated Sorted Array and the duplicate-tolerant variant of this problem.

Asked at

AmazonGoogleMicrosoftMetaBloombergAdobe
Frequently Sometimes Occasionally
Example 1
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
The sorted array [0,1,2,4,5,6,7] was rotated at pivot 4; the value 0 now sits at index 4.
Example 2
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
3 does not appear anywhere in the array.
Example 3
Input: nums = [1], target = 0
Output: -1
A single-element array that does not contain the target.
Constraints

- 1 <= nums.length <= 5000 - -10^4 <= nums[i] <= 10^4 - All values of nums are unique - nums is an ascending array that is possibly rotated - -10^4 <= target <= 10^4

Solve this problem →