Search in Rotated Sorted Array
Search in Rotated Sorted Array is a medium binary search problem solved with the rotated array pattern.
The best approach, optimal (find the sorted half), runs in O(log n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from linear scan up.
Problem
A sorted array of distinct integers was rotated at an unknown pivot. Given a target, return its index or -1, in O(log n) time.
Examples
Example 1
- Input
nums = [5, 6, 7, 0, 1, 2, 4], target = 1- Output
4
Example 2
- Input
nums = [5, 6, 7, 0, 1, 2, 4], target = 3- Output
-1
Constraints
1 <= nums.length <= 5000, distinct values.- O(log n).
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Linear scan | O(n) | O(1) |
| Optimal (find the sorted half) | O(log n) | O(1) |
1Linear scan
O(n)O(1)Check every element.
- Return the index of target or -1.
class Solution {
public int search(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) if (nums[i] == target) return i;
return -1;
}
}class Solution:
def search(self, nums: List[int], target: int) -> int:
for i, x in enumerate(nums):
if x == target:
return i
return -1class Solution {
public:
int search(vector<int>& nums, int target) {
for (int i = 0; i < (int)nums.size(); i++) if (nums[i] == target) return i;
return -1;
}
};function search(nums, target) {
for (let i = 0; i < nums.length; i++) if (nums[i] === target) return i;
return -1;
}2Optimal (find the sorted half)
O(log n)O(1)If nums[lo] <= nums[mid], the left half is sorted: search it when target is in [nums[lo], nums[mid]), otherwise go right. If not, the right half is sorted: search it when target is in (nums[mid], nums[hi]], otherwise go left.
- If nums[mid] == target, return mid.
- Left sorted: nums[lo] <= target < nums[mid] ? hi = mid - 1 : lo = mid + 1.
- Right sorted: nums[mid] < target <= nums[hi] ? lo = mid + 1 : hi = mid - 1.
class Solution {
public int search(int[] nums, int target) {
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (nums[mid] == target) return mid;
if (nums[lo] <= nums[mid]) {
if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
} else {
if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
}class Solution:
def search(self, nums: List[int], target: int) -> int:
lo, hi = 0, len(nums) - 1
while lo <= hi:
mid = (lo + hi) // 2
if nums[mid] == target:
return mid
if nums[lo] <= nums[mid]:
if nums[lo] <= target < nums[mid]:
hi = mid - 1
else:
lo = mid + 1
else:
if nums[mid] < target <= nums[hi]:
lo = mid + 1
else:
hi = mid - 1
return -1class Solution {
public:
int search(vector<int>& nums, int target) {
int lo = 0, hi = (int)nums.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) return mid;
if (nums[lo] <= nums[mid]) {
if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
} else {
if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
};function search(nums, target) {
let lo = 0, hi = nums.length - 1;
while (lo <= hi) {
const mid = (lo + hi) >>> 1;
if (nums[mid] === target) return mid;
if (nums[lo] <= nums[mid]) {
if (nums[lo] <= target && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
} else {
if (nums[mid] < target && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}Edge cases to test
- Not rotated
- Target at the rotation point
Hints
Hint 1
At any mid, at least one of the halves [lo, mid] and [mid, hi] is sorted. Check whether the target falls in that sorted half.
FAQ
What is the best time complexity for Search in Rotated Sorted Array?
Optimal (find the sorted half) runs in O(log n) time and O(1) extra space.
Which pattern does Search in Rotated Sorted Array use?
It is a binary search problem that uses the rotated array pattern. Other problems with the same pattern: Find How Many Times Array is Rotated, Find Minimum in Rotated Sorted Array, Find Minimum in Rotated Sorted Array II.
Is there a brute force solution for Search in Rotated Sorted Array?
Yes. Linear scan takes O(n) time and O(1) space. Check every element.
Which edge cases should I test for Search in Rotated Sorted Array?
Not rotated; Target at the rotation point.