LeetCode #33 Medium

Search in Rotated Sorted Array

Search in Rotated Sorted Array is LeetCode 33 (Medium). An array of distinct integers was sorted in ascending order and then possibly rotated: cut at some index, with the two pieces swapped. Given target, return its index, or −1 if it is not there.

  • The solution must run in O(log n) time.
  • A rotation by 0 is allowed, so the array may simply be sorted.
Constraints
  • 1 <= nums.length <= 5000
  • -10⁴ <= nums[i] <= 10⁴
  • All values of nums are unique.
  • nums is an ascending array that is possibly rotated.
  • -10⁴ <= target <= 10⁴
binary-searcharray
Open on LeetCode ↗
02

Intuition

A rotated array has exactly one drop, where a big value is followed by a small one. Split the range at the middle and the drop can only be on one side, so at least one half is fully sorted.

A sorted half is easy to question: target is in it exactly when it lies between that half's first and last values. So each step does two things:

  • Find the sorted half by comparing the left end with the middle.
  • Ask whether the target fits inside it. If it does, search there; if not, it can only be in the other half.

Either way half the range is gone.

How to spot this pattern

A sorted array that was rotated, with a request for a value's index in O(log n): find the sorted side of the middle, then range-check the target against it.

03

Approach

Try it first

Before reading on, split a rotated array at its middle and mark which side has no drop. How do you tell that side from the values at the left end and the middle alone? Then build an O(log n) search on that test.

1

Check the middle first

With lo = 0, hi = n - 1 and mid between them, return mid if nums[mid] == target. The range tests below then only need to decide where the target is not.

2

Left half sorted

If nums[lo] <= nums[mid], the values from lo to mid rise with no drop. When nums[lo] <= target < nums[mid] the target can only be among them, so set hi = mid - 1; otherwise set lo = mid + 1.

3

Right half sorted

Otherwise the drop sits between lo and mid, so the values from mid to hi rise. When nums[mid] < target <= nums[hi], set lo = mid + 1; otherwise set hi = mid - 1.

4

An empty range means absent

Loop while lo <= hi. Every pass removes the middle and a half that cannot hold the target, so once the range is empty, return −1.

04

Code in Python, C++ and Java

▶1from typing import List
▶2 
▶3 
▶4class Solution:
▶5 def search(self, nums: List[int], target: int) -> int:
▶6 lo, hi = 0, len(nums) - 1
▶7 while lo <= hi:
▶8 mid = (lo + hi) // 2
▶9 if nums[mid] == target:
▶10 return mid
▶11 if nums[lo] <= nums[mid]:
▶12 if nums[lo] <= target < nums[mid]:
▶13 hi = mid - 1
▶14 else:
▶15 lo = mid + 1
▶16 else:
▶17 if nums[mid] < target <= nums[hi]:
▶18 lo = mid + 1
▶19 else:
▶20 hi = mid - 1
▶21 return -1
40lo516273041526hitarget0search indices 0 to 6 for 0
lo0
hi6
target0
The values sit around a ring in index order. Going clockwise they rise, drop once, and rise again: that one drop is where the sorted array was cut and the pieces swapped.
40lo516273mid041526hisorted left4to7left end 4 ≤ middle 7 → left half is sorted
mid3
nums[lo]4
nums[mid]7
The middle holds 7, not 0. 4 is not bigger than 7, so there is no drop between indices 0 and 3: they rise from 4 to 7.
4051627304lo1526hisorted left4to70 is not between 4 and 7 → keep 4 to 6
lo4
hi6
0 falls outside 4 to 7, so the sorted half cannot hold it. Whatever is left can only be on the other side of the middle.
4051627304lo15mid26hisorted left0to1left end 0 ≤ middle 1 → left half is sorted
mid5
nums[lo]0
nums[mid]1
The middle holds 1, not 0. 0 is not bigger than 1, so there is no drop between indices 4 and 5: they rise from 0 to 1.
4051627304lo hi1526sorted left0to10 is between 0 and 1 → keep index 4
lo4
hi4
A sorted run holds exactly the values between its ends, and 0 is one of them, so it can only be in this half. The other half and the middle are dropped.
4051627304lo mid hi1526target0index 4 holds 0 → answer 4
mid4
answer4
The middle value is the target, so return its index, 4.
05

Common pitfalls

A strict comparison in the sorted-half test

✗ Wrong
if nums[lo] < nums[mid]:
✓ Right
if nums[lo] <= nums[mid]:

With two values left, mid equals lo, and that one-value left half counts as sorted. The strict test calls the right half sorted instead. On [3, 1] with target 1, it asks whether 1 lies above 3 and at most 1, which is impossible, so it drops the 1 and returns −1.

Leaving the left end out of the range test

✗ Wrong
if nums[lo] < target < nums[mid]:
✓ Right
if nums[lo] <= target < nums[mid]:

The first value of the sorted half can be the target. On [4, 5, 6, 7, 0, 1, 2] with target 4, the left half 4 to 7 is sorted, but 4 < 4 fails, so the search moves right, away from index 0, and returns −1.

06

Edge cases

Not rotated at all, e.g. [1, 3, 5, 7]

The left end is never bigger than the middle, so the left half always counts as sorted and the loop behaves exactly like a plain binary search.

07

Complexity

Time
O(log n)
Space
O(1)
Each pass halves the range, so 5000 values need at most 13 passes. Only lo, hi and mid are stored.
08

Search in Rotated Sorted Array FAQ

Can I find the rotation point first and then do a normal binary search?

Yes. Locate the smallest value as in Find Minimum in Rotated Sorted Array, then binary search whichever of the two sorted pieces could hold the target. Both loops are O(log n); the one-pass version just avoids writing two.

What breaks if the array can contain repeated values?

When the left end and the middle hold the same value, that comparison no longer shows which half is sorted, and the search has to fall back to shrinking the range one value at a time. That version is Search in Rotated Sorted Array II, which only asks whether the target exists.