Skip to content
AI360Xpert
Beta
Difficulty: MediumBinary Search

Search in Rotated Sorted Array

Problem in Plain English

You are given a rotated sorted array of distinct integers and a target value. Return the index of the target in the array, or negative one when the target is absent.

Problem Statement

There is an integer array nums sorted in ascending order with distinct values. Before it reaches you, nums is possibly rotated at an unknown pivot index, so the tail of the sorted order may wrap to the front.

Given the possibly rotated array nums and an integer target, return the index of target if it is in nums, or -1 if it is not. The required solution runs in O(logn)O(log n) time.

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

Examples

Example 1

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

The value 0 sits at index 4, so the answer is 4.

Example 2

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

The target 3 lies in the gap between 2 and 4 in the original sorted order. Rotation changes positions but adds no values, so the candidate interval eventually becomes empty and the answer is -1.

Example 3

Input
nums = [1], target = 0
Output
-1

The single element 1 is not the target, so the answer is -1.

Example 4

Input
nums = [3,1], target = 1
Output
1

The midpoint is initially index 0. Its one-element left half is sorted, but target 1 is outside it, so the search retains index 1 and finds the match. This exercises inclusive half detection on two elements.

Intuition

Rotation cuts one ascending sequence into two ascending pieces and swaps their order. In Example 1, [4,5,6,7] is followed by [0,1,2], so comparing only the midpoint 7 with target 0 does not tell us which side to keep. At each midpoint, however, at least one half has no wrap and is sorted. Its endpoints tell us whether target belongs in that half; otherwise we keep the other half.

Approaches

Linear Scan

Solution Details

Reveal Linear Scan: intuition, complexity, and code

Hints

Hint 1
Split any interval at mid: at least one of the two halves must be in sorted order, so test it with nums[left] <= nums[mid].
Hint 2
Once the sorted half is known, check whether target sits between its endpoints before deciding which half to drop.
Hint 3
Keep the left <= right loop with mid - 1 and mid + 1 updates so a single remaining element is still examined.

Edge Cases

  • A single-element array such as [1] with a missing target returns -1 after one comparison.
  • An unrotated array behaves like plain binary search because the left half is sorted every time.
  • A target sitting exactly at the rotation dip, like 0 in [4,5,6,7,0,1,2], is found once the interval narrows to it.
  • Two elements with rotation, such as [3,1], require treating left == mid as a sorted one-element half.
  • Targets at either endpoint must remain eligible: the sorted-half range includes nums[left] or nums[right], while mid was already checked for equality.
  • Negative values and zero affect comparisons only; the same half-detection rule applies.

Common Mistakes and Interview Tips

  • Testing whether the target is in range before confirming which half is sorted, which discards the wrong side.
  • Using strict < instead of <= for the sorted-half test, which misclassifies a one-element half.
  • Updating bounds with mid instead of mid - 1 or mid + 1, which can loop forever on two elements.
  • Sorting the input before searching changes the indices you are asked to return and costs O(n log n) time.
  • Using ordinary binary search solely from nums[mid] < target ignores the rotation and can discard a present target.
  • Excluding the endpoint in nums[left] <= target or target <= nums[right] loses matches at the retained half's boundary.
  • Reusing this distinct-value algorithm for duplicate input such as [1,0,1,1,1] can hide the pivot; equal endpoints require additional handling.

Key Takeaway

When rotation breaks global order, identify the sorted half of each interval and keep only the side that can still hold the target.