Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get nums, which was a sorted array of distinct numbers and then got rotated: some elements from the end were moved to the front, like [0, 1, 2, 4, 5, 6, 7] becoming [4, 5, 6, 7, 0, 1, 2]. You also get a target. Return its index, or -1 if it isn't there, in O(log n) time. Up to 5000 elements.
Examples with nums = [4, 5, 6, 7, 0, 1, 2]: target = 0 → 4, and target = 3 → -1.
Start with brute force
Scan the array and return the index where nums[i] == target. That's O(n), up to 5000 steps. It works, but it ignores that the array is two sorted pieces, and the problem asks for O(log n).
Building the intuition
You can't binary search the whole array with "is nums[i] >= target?", because it isn't sorted. For target = 5 that question gives F T T T F F F, which isn't falses-then-trues.
But you've already solved the hard part. In Find Minimum in Rotated Sorted Array, you found the minimum in O(log n). Look at what's on each side of it:
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| nums[i] | 4 | 5 | 6 | 7 | 0 | 1 | 2 |
| piece | left | left | left | left | right | right | right |
Both pieces are sorted. So if you knew which piece holds the target, a normal binary search on that piece would finish the job.
Which piece? Use the last element again, last = 2. Every number in the right piece is <= 2. Every number in the left piece is > 2. So:
target <= last→ it can only be in the right piece,[pivot, n).target > last→ it can only be in the left piece,[0, pivot).
That's the whole solution: two searches with the same template.
The optimal approach
- Find the pivot. With
last = nums[n - 1], find the first index wherenums[i] <= last. Uselo = 0,hi = n - 1, exactly as in Find Minimum in Rotated Sorted Array. Call itpivot. - Pick the piece. If
target <= last, searchlo = pivot,hi = n. Otherwise searchlo = 0,hi = pivot. Remember that right edge asend. It's the piece's past-the-end slot. - Lower bound. While
lo < hi:mid = lo + (hi - lo) // 2. Ifnums[mid] >= target, sethi = mid, elselo = mid + 1. - Check. If
lo < endandnums[lo] == target, returnlo. Otherwise return-1.
The subtle detail: the unrotated case. If the array was never rotated, pivot = 0, so the left piece [0, 0) is empty. A target bigger than last is bigger than everything, so it's correctly not found: the search starts with lo = hi = 0 = end, and the check lo < end fails. No special case.
You'll also see a one-loop version that, at each mid, works out which half is sorted and whether the target lies inside it. It's also O(log n), but it has four branches and is a common place for off-by-one bugs. The two-search version reuses a loop you've already proved correct.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
last = nums[-1]
# Step 1: the pivot is the first index with nums[i] <= last (the minimum).
# Left of it every value is > last; from it onwards every value is <= last.
lo, hi = 0, n - 1 # the last index is always T
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[mid] <= last:
hi = mid
else:
lo = mid + 1
pivot = lo
# Step 2: comparing with last tells us which sorted piece can hold target.
if target <= last:
lo, hi = pivot, n # right piece [pivot, n)
else:
lo, hi = 0, pivot # left piece [0, pivot); empty if not rotated
end = hi # this piece's past-the-end slot
# Step 3: ordinary lower bound on that piece.
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[mid] >= target:
hi = mid
else:
lo = mid + 1
if lo < end and nums[lo] == target:
return lo
return -1class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
int last = nums[n - 1];
// Step 1: the pivot is the first index with nums[i] <= last (the minimum).
// Left of it every value is > last; from it onwards every value is <= last.
int lo = 0, hi = n - 1; // the last index is always T
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] <= last) {
hi = mid;
} else {
lo = mid + 1;
}
}
int pivot = lo;
// Step 2: comparing with last tells us which sorted piece can hold target.
if (target <= last) {
lo = pivot; hi = n; // right piece [pivot, n)
} else {
lo = 0; hi = pivot; // left piece [0, pivot); empty if not rotated
}
int end = hi; // this piece's past-the-end slot
// Step 3: ordinary lower bound on that piece.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] >= target) {
hi = mid;
} else {
lo = mid + 1;
}
}
if (lo < end && nums[lo] == target) return lo;
return -1;
}
};class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int last = nums[n - 1];
// Step 1: the pivot is the first index with nums[i] <= last (the minimum).
// Left of it every value is > last; from it onwards every value is <= last.
int lo = 0, hi = n - 1; // the last index is always T
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] <= last) {
hi = mid;
} else {
lo = mid + 1;
}
}
int pivot = lo;
// Step 2: comparing with last tells us which sorted piece can hold target.
if (target <= last) {
lo = pivot; hi = n; // right piece [pivot, n)
} else {
lo = 0; hi = pivot; // left piece [0, pivot); empty if not rotated
}
int end = hi; // this piece's past-the-end slot
// Step 3: ordinary lower bound on that piece.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] >= target) {
hi = mid;
} else {
lo = mid + 1;
}
}
if (lo < end && nums[lo] == target) return lo;
return -1;
}
}/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function (nums, target) {
const n = nums.length;
const last = nums[n - 1];
// Step 1: the pivot is the first index with nums[i] <= last (the minimum).
// Left of it every value is > last; from it onwards every value is <= last.
let lo = 0, hi = n - 1; // the last index is always T
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (nums[mid] <= last) {
hi = mid;
} else {
lo = mid + 1;
}
}
const pivot = lo;
// Step 2: comparing with last tells us which sorted piece can hold target.
if (target <= last) {
lo = pivot; hi = n; // right piece [pivot, n)
} else {
lo = 0; hi = pivot; // left piece [0, pivot); empty if not rotated
}
const end = hi; // this piece's past-the-end slot
// Step 3: ordinary lower bound on that piece.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (nums[mid] >= target) {
hi = mid;
} else {
lo = mid + 1;
}
}
if (lo < end && nums[lo] === target) return lo;
return -1;
};Dry run
nums = [4, 5, 6, 7, 0, 1, 2], last = 2.
Step 1, find the pivot (nums[mid] <= 2?):
| lo | mid | hi | nums[mid] | <= 2? | update |
|---|---|---|---|---|---|
| 0 | 3 | 6 | 7 | No | lo = 4 |
| 4 | 5 | 6 | 1 | Yes | hi = 5 |
| 4 | 4 | 5 | 0 | Yes | hi = 4 |
| 4 | 4 | pivot = 4 |
Target 0. 0 <= 2, so search the right piece: lo = 4, hi = end = 7. Question: nums[mid] >= 0?
| lo | mid | hi | nums[mid] | >= 0? | update |
|---|---|---|---|---|---|
| 4 | 5 | 7 | 1 | Yes | hi = 5 |
| 4 | 4 | 5 | 0 | Yes | hi = 4 |
| 4 | 4 | stop |
4 < 7 and nums[4] = 0. Return 4.
Target 3. 3 > 2, so search the left piece: lo = 0, hi = end = 4. Question: nums[mid] >= 3?
| lo | mid | hi | nums[mid] | >= 3? | update |
|---|---|---|---|---|---|
| 0 | 2 | 4 | 6 | Yes | hi = 2 |
| 0 | 1 | 2 | 5 | Yes | hi = 1 |
| 0 | 0 | 1 | 4 | Yes | hi = 0 |
| 0 | 0 | stop |
nums[0] = 4, not 3. Return -1.
Complexity
- Time: O(log n). Two binary searches, each O(log n). Two times log n is still O(log n): about 26 steps for 5000 elements.
- Space: O(1). A handful of integers.
Edge cases and traps
- Not rotated.
pivot = 0. Targets<= lastsearch the whole array; bigger targets search an empty piece and return-1. - One element.
pivot = 0. Iftarget <= nums[0], search[0, 1); otherwise the left piece is empty. Both give the right answer. - Target equals
last. It belongs to the right piece: use<=, not<, when picking. - Picking the piece with
target >= nums[0]. It looks symmetric, but in an unrotated array like[1, 2, 3]every target>= 1gets sent to the left piece[0, 0), which is empty, so2is "not found". Comparing with the last element has no such hole. - Duplicates. This problem promises distinct values. With duplicates, the pivot search breaks; see the follow-up.
Say it in the interview
"A rotated sorted array is two sorted pieces. First I binary search for where the second piece starts: the first index whose value is at most the last element. Everything in the second piece is at most the last element and everything in the first piece is bigger, so comparing the target with the last element tells me which piece to search. Then I run a normal lower bound on that piece and check that the index holds the target. Two binary searches, so O(log n) time and O(1) space."
Follow-ups interviewers ask
- "Can you do it in a single pass?" Yes: at each
mid, one of the halves[lo, mid]or[mid, hi]is sorted. Check whether the target lies inside the sorted half's range; if so go there, else go to the other half. Same O(log n), just one loop. - "What if there are duplicates?" That's Search in Rotated Sorted Array II. With
[1, 1, 1, 3, 1], you can't tell the pieces apart by comparing withlast. The usual fix shrinks the range by one when the ends are equal, which is O(n) in the worst case. - "Return where it was in the original, unrotated array?" It's
(index - pivot + n) % n.
Practise the same idea
This is Day 41 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free