A list of distinct integers was sorted from smallest to largest. Then it may have been rotated: some first part of the list was cut off and moved to the end. So nums reads as two sorted runs, such as [30, 40, 50, 10, 20], or as one fully sorted run if nothing was moved.
Given nums and a number target, return the index where target sits, or -1 when it does not appear. Your search should take O(log n) time.
Example 1
Input: nums = [22, 27, 31, 3, 7, 11, 15], target = 7 Output: 4 Why: 7 sits in the second sorted run, at index 4
2 more examples and the constraints
Example 2
Input: nums = [22, 27, 31, 3, 7, 11, 15], target = 20 Output: -1 Why: 20 is not in the list
Example 3
Input: nums = [9], target = 9 Output: 0 Why: the only value is the target
Constraints
- 1 ≤ length of nums ≤ 5,000
- −10,000 ≤ nums[i], target ≤ 10,000
- No value appears twice
- nums was sorted in increasing order, then possibly rotated
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Linear scanO(n)
- Find the rotation, then binary searchO(log n)
- One binary search, sorted halfO(log n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 5,000 values | Verdict |
|---|---|---|---|
| Linear scan | O(n) | Correct, but ignores the order | |
| Find the rotation, then binary search | O(log n) | Passes; two simple searches | |
| One binary search, sorted half | O(log n) | Best; one loop, no setup |
n = how many values are in nums.
Edge cases to test
- No rotation. [1, 4, 6, 8, 12] is one sorted run.
nums[lo] ≤ nums[mid]always holds, and the search becomes a plain binary search. - One element. [9] has lo = mid = hi = 0. Return 0 if it matches, otherwise -1.
- Target at the pivot. In [40, 50, 60, 10, 20, 30], 10 is the smallest value, right after the drop. Index 3.
- Target missing. A value that falls in a gap, like 20 in Example 2, must end with an empty window and -1, not a loop that never stops.
- Two elements. In [5, 2], lo and mid are the same index, so
nums[lo] ≤ nums[mid]must use ≤, not <.
Common mistakes
- Writing
nums[lo] < nums[mid]instead of ≤. When lo equals mid, the one-element left half is sorted, and the strict check sends the search the wrong way. - Using the wrong ends in the range check: the sorted left half is
nums[lo] ≤ target < nums[mid], the sorted right half isnums[mid] < target ≤ nums[hi]. - Deciding which half to keep by comparing the target with
nums[mid]alone, as in a normal binary search. In a rotated array that is not enough.
How to talk through it
- 1Clarify. Are the values distinct? Could the array be not rotated at all? What do I return when the target is missing?
- 2Give the linear scan. One pass is O(n). Say that it ignores the sorted order, and the problem wants O(log n).
- 3Name the key fact. Split a rotated array at any index and at least one half is sorted. A sorted half lets you test the target with one range check.
- 4Code the one loop. Check mid, decide which half is sorted with
nums[lo] ≤ nums[mid], then keep the half that can hold the target. - 5Test out loud. Run Example 1, an unrotated array, a single element and a missing target before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?