There is an integer array nums
sorted in ascending order (with distinct values).
Prior to being passed to your function, nums
is possibly rotated at an unknown pivot index k
(1 <= k < nums.length
) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
(0-indexed). For example, [0,1,2,4,5,6,7]
might be rotated at pivot index 3
and become [4,5,6,7,0,1,2]
.
Given the array nums
after the possible rotation and an integer target
, return the index of target
if it is in nums
, or -1
if it is not in nums
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1
Example 3:
Input: nums = [1], target = 0 Output: -1
Constraints:
1 <= nums.length <= 5000
-104 <= nums[i] <= 104
nums
are unique.nums
is an ascending array that is possibly rotated.-104 <= target <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Think of searching for a specific number in a book where the pages are almost in order, but a section has been shifted. The brute force method simply looks at every single number in the book one by one until you find the number you're looking for or you've checked them all.
Here's how the algorithm would work step-by-step:
def search_in_rotated_sorted_array_brute_force(numbers, target):
# Iterate through each number in the array.
for current_index in range(len(numbers)):
# Check if the current number matches the target.
if numbers[current_index] == target:
return current_index
# If the target is not found in the array,
# return -1 to indicate it's not present.
return -1
The problem deals with finding a specific value in a list that was originally sorted but then rotated. Instead of checking each list item individually, we leverage the fact that at least *part* of the list remains sorted to guide our search more efficiently.
Here's how the algorithm would work step-by-step:
def search_in_rotated_sorted_array(nums, target):
left_pointer = 0
right_pointer = len(nums) - 1
while left_pointer <= right_pointer:
middle_pointer = (left_pointer + right_pointer) // 2
if nums[middle_pointer] == target:
return middle_pointer
# Check if the left side is sorted
if nums[left_pointer] <= nums[middle_pointer]:
# If target is in the sorted left portion.
if nums[left_pointer] <= target < nums[middle_pointer]:
right_pointer = middle_pointer - 1
# Otherwise, target is in the unsorted right.
else:
left_pointer = middle_pointer + 1
# The left side is not sorted, so the right side must be.
else:
# If the target is in the sorted right portion
if nums[middle_pointer] < target <= nums[right_pointer]:
# Target is located on the right side.
left_pointer = middle_pointer + 1
# Otherwise, the target is in the unsorted left portion.
else:
# Target must be on the left side.
right_pointer = middle_pointer - 1
# Target is not in the array.
return -1
Case | How to Handle |
---|---|
Empty array | Return -1 immediately as the target cannot be found in an empty array. |
Array with one element and target matches | Return index 0 if the single element equals the target. |
Array with one element and target does not match | Return -1 if the single element does not equal the target. |
Array with two elements and target is one of them | Return the index of the element matching the target. |
Array with two elements and target is not in the array | Return -1 as the target is not present. |
Array rotated such that it is still sorted (no rotation) | Binary search will work as expected on the sorted array. |
Target is smaller than the minimum value or larger than the maximum value in the array | The binary search will naturally converge to -1 since the target isn't within the sorted segments. |
Array contains duplicate values near the pivot point, potentially masking the rotation | Binary search with comparisons to both left and right elements will correctly narrow down the search space despite duplicates. |