33. Search in Rotated Sorted Array
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to find a target value in a sorted integer array that has been rotated at an unknown pivot. The values in the array are distinct. The crucial constraint is that the algorithm must have a time complexity of O(log n), which strongly suggests a binary search approach. A standard binary search won't work directly because the array is not fully sorted. However, the rotation preserves a key property: the array consists of two sorted subarrays. For any middle element nums[mid], at least one of the halves (left part [left...mid] or right part [mid...right]) must be sorted. We can leverage this property to adapt the binary search.
Solution
Search in Rotated Sorted Array in Python
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
# Calculate mid point to avoid potential overflow
mid = left + (right - left) // 2
# Target found
if nums[mid] == target:
return mid
# Check if the left half [left, mid] is sorted
if nums[left] <= nums[mid]:
# Check if the target is within the sorted left half
if nums[left] <= target < nums[mid]:
# Search in the left half
right = mid - 1
else:
# Search in the right half
left = mid + 1
else:
# The right half [mid, right] must be sorted
# Check if the target is within the sorted right half
if nums[mid] < target <= nums[right]:
# Search in the right half
left = mid + 1
else:
# Search in the left half
right = mid - 1
# Target not found
return -1
Search in Rotated Sorted Array in Java
class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
// Calculate mid point to avoid potential overflow
int mid = left + (right - left) / 2;
// Target found
if (nums[mid] == target) {
return mid;
}
// Check if the left half [left, mid] is sorted
if (nums[left] <= nums[mid]) {
// Check if the target is within the sorted left half
if (target >= nums[left] && target < nums[mid]) {
// Search in the left half
right = mid - 1;
} else {
// Search in the right half
left = mid + 1;
}
} else {
// The right half [mid, right] must be sorted
// Check if the target is within the sorted right half
if (target > nums[mid] && target <= nums[right]) {
// Search in the right half
left = mid + 1;
} else {
// Search in the left half
right = mid - 1;
}
}
}
// Target not found
return -1;
}
}
Search in Rotated Sorted Array in C++
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
// Calculate mid point to avoid potential overflow
int mid = left + (right - left) / 2;
// Target found
if (nums[mid] == target) {
return mid;
}
// Check if the left half [left, mid] is sorted
if (nums[left] <= nums[mid]) {
// Check if the target is within the sorted left half
if (target >= nums[left] && target < nums[mid]) {
// Search in the left half
right = mid - 1;
} else {
// Search in the right half
left = mid + 1;
}
} else {
// The right half [mid, right] must be sorted
// Check if the target is within the sorted right half
if (target > nums[mid] && target <= nums[right]) {
// Search in the right half
left = mid + 1;
} else {
// Search in the left half
right = mid - 1;
}
}
}
// Target not found
return -1;
}
};
Search in Rotated Sorted Array in TypeScript
function search(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
// Calculate mid point to avoid potential overflow
let mid = left + Math.floor((right - left) / 2);
// Target found
if (nums[mid] === target) {
return mid;
}
// Check if the left half [left, mid] is sorted
if (nums[left] <= nums[mid]) {
// Check if the target is within the sorted left half
if (target >= nums[left] && target < nums[mid]) {
// Search in the left half
right = mid - 1;
} else {
// Search in the right half
left = mid + 1;
}
} else {
// The right half [mid, right] must be sorted
// Check if the target is within the sorted right half
if (target > nums[mid] && target <= nums[right]) {
// Search in the right half
left = mid + 1;
} else {
// Search in the left half
right = mid - 1;
}
}
}
// Target not found
return -1;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.