LeetCode 33 · Medium
Search in Rotated Sorted Array Explained (LeetCode 33)
Updated Nov 27, 2026 · 3 min read
Cut a sorted list anywhere and at least one half around the middle is still in order. Peek at the middle, work out which half is sorted, and check whether the target fits inside that half's range. If it does, search there; if not, search the other half. That's binary search, O(log n) time and O(1) space.
Key points
- ●Checking every item is O(n); the problem wants O(log n).
- ●After a rotation, one half around the middle is always sorted.
- ●If nums[left] <= nums[mid], the left half is sorted; otherwise the right half is.
- ●Ask if the target fits in the sorted half's range, then keep that half or drop it.
- ●About 20 peeks for a million items: O(log n) time, O(1) space.

The riddle: book 7 on Nia's cut shelf
Nia's books were in order, smallest number to biggest. Then someone cut the shelf and moved the front chunk to the back. Now it reads 15, 18, 22, 3, 7, 11. She needs book 7.
That's LeetCode 33, Search in Rotated Sorted Array: a list of distinct numbers was sorted, then rotated at some unknown point. Given the list and a target, return the target's index, or -1 if it isn't there, in O(log n) time. Here the answer is 4, because nums[4] is 7.
The slow way: check every book
Walk the shelf from the left until you find 7. It works, but a million books can mean a million looks. That's O(n), and the problem asks for O(log n), which means every decision has to throw away about half of what's left.
Plain binary search doesn't work directly either, because the list isn't fully sorted any more. If you compare 7 with the middle book, 22, ordinary binary search says "go left", and the left side is 15 and 18. You'd never find it.
The idea: one half is always in order
Here's Nia's trick. Cut a sorted shelf anywhere, pick the middle book, and at least one side of it is still in order. The break can only be on one side.
So each round:
- Peek at the middle. If it's the target, you're done.
- Find the sorted half. If
nums[left] <= nums[mid], the left half is in order; otherwise the right half is. - A sorted half has a known range, from its smallest end to its biggest. Ask: does the target fit inside that range?
- Yes? Search there. No? It can't be there, so search the other half.
Step 3 of the visual sheet above shows the sorted-half check, and step 4 shows the range test.
Trace it on Nia's shelf
| Round | left | mid | right | nums[mid] | What happens |
|---|---|---|---|---|---|
| 1 | 0 | 2 | 5 | 22 | 15 <= 22, left half sorted. Is 7 in [15, 22)? No. Move left to 3. |
| 2 | 3 | 4 | 5 | 7 | Found it. Return 4. |
Two peeks. A million books takes about twenty.
The code in Python
def search(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
search([15, 18, 22, 3, 7, 11], 7) returns 4. Ask for 5 instead and the window empties, so it returns -1.
Complexity
- Time: O(log n). Each round removes half of the remaining window.
- Space: O(1). Only
left,midandright.
From O(n) down to O(log n), with the same loop shape as ordinary binary search.
The trap to watch for
The boundaries. Use <= in nums[left] <= nums[mid]: when the window shrinks to one or two items, left and mid can be the same index, and a strict < sends you to the wrong half. In the range tests, include the sorted half's outer end (nums[left] <= and <= nums[right]) but not mid, because you already checked mid at the top of the loop.
The same loop also handles a one-item list, a missing target and a list that was never rotated at all.
Frequently asked questions
How do you know which half is sorted in a rotated array?
Compare the left end with the middle. If nums[left] <= nums[mid], nothing breaks between them, so the left half is sorted. Otherwise the break is in the left half, which means the right half is sorted.
What is the time complexity of Search in Rotated Sorted Array?
O(log n) time, because every round throws away about half of what's left, and O(1) space, because you only track left, mid and right. A million items take about 20 rounds.
Why does the problem need distinct values?
The sorted-half test relies on it. With duplicates, nums[left] and nums[mid] can be equal even when the break sits between them, and you can no longer tell which half is in order.
How is this different from Find Minimum in Rotated Sorted Array?
Both use the same sorted-half idea. LeetCode 153 hunts for the break point to find the smallest value. LeetCode 33 uses the sorted half to decide whether a given target could be there.