33. Search in Rotated Sorted Array¶
Edited by Charles
Medium · Python · Array
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Use Binary Search.
Although the rotated array is not completely sorted, after splitting the current interval at mid, at least one half must be sorted.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
Use Binary Search.
Although the rotated array is not completely sorted, after splitting the current interval at mid, at least one half must be sorted.
For example:
If:
then:
The left half is sorted.
Therefore, in every iteration:
- Determine which half is sorted.
- Check whether
targetlies inside that sorted half. - Decide which half to continue searching.
Step 1: Check Whether Target Is Found¶
As in normal binary search:
Step 2: Determine Which Half Is Sorted¶
Left Half Is Sorted¶
If:
then the interval:
is sorted.
For example:
Since:
the left half:
is sorted.
Why Use <= Instead of <?¶
We must use:
instead of:
because left can be equal to mid.
For example:
Now:
and:
The interval [left, mid] contains only one element, so it is still sorted.
Therefore:
correctly identifies the left half as sorted.
Step 3: Check Whether Target Is Inside the Sorted Half¶
Case 1: Left Half Is Sorted¶
We know:
Now check whether target is inside this sorted interval:
If true:
Otherwise:
So:
Left half is sorted
nums[left] <= target < nums[mid]
↓
target is in the left half
↓
right = mid - 1
Otherwise
↓
target must be in the right half
↓
left = mid + 1
Case 2: Right Half Is Sorted¶
If the left half is not sorted, then the right half must be sorted.
Check whether target lies inside:
If true:
Otherwise:
So:
Right half is sorted
nums[mid] < target <= nums[right]
↓
target is in the right half
↓
left = mid + 1
Otherwise
↓
target must be in the left half
↓
right = mid - 1
Example¶
Round 1¶
We have:
Since:
the left half is sorted:
Check whether target = 0 is inside:
False.
Therefore, search the right half:
Now:
Round 2¶
Now:
Since:
the left half is sorted.
Check:
For target = 0:
True.
Therefore:
Now:
Round 3¶
Therefore:
Binary Search Logic¶
The entire logic can be summarized as:
nums[mid] == target
↓
return mid
Which half is sorted?
/ \
/ \
Left Right
↓ ↓
Is target Is target
inside it? inside it?
/ \ / \
Yes No Yes No
↓ ↓ ↓ ↓
Left Right Right Left
The key idea is:
Find the sorted half first, then determine whether the target lies inside that sorted half.
Complexity¶
Time Complexity:
Each iteration eliminates half of the current search interval.
Space Complexity:
Key Points¶
At least one half is sorted¶
After choosing mid, either:
or:
must be sorted.
Determine the sorted half first¶
Then check whether target is inside it¶
Left sorted:
Right sorted:
Use <=¶
because when:
the left interval contains one element and is still sorted.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 8, 2026 |
Python solution¶
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
# Left half is sorted
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
# Right half is sorted
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1