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] <= 104nums are unique.nums is an ascending array that is possibly rotated.-104 <= target <= 104When 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:
Imagine you have a row of items that has been split and shifted around, and you need to find a specific item hidden inside. The brute force approach simply checks every single item one by one until you find the one you are looking for.
Here's how the algorithm would work step-by-step:
def search_rotated_array(nums, target):
# We iterate through every element because the array is rotated and unsorted for a linear scan
for current_index in range(len(nums)):
# Check current element against target to see if we found a match
if nums[current_index] == target:
return current_index
# Exhausted all elements without finding the target so return negative one
return -1Even though the sequence of numbers has been shifted and broken, we can still use a clever guessing game by always looking right at the middle. Because one half of any section is guaranteed to be in normal order, we can easily figure out which side our target must be hiding in.
Here's how the algorithm would work step-by-step:
def search(nums_array, target_value):
left_pointer = 0
right_pointer = len(nums_array) - 1
while left_pointer <= right_pointer:
middle_index = (left_pointer + right_pointer) // 2
if nums_array[middle_index] == target_value:
return middle_index
# Identify if the left half is normally sorted to determine search boundaries.
if nums_array[left_pointer] <= nums_array[middle_index]:
# Check if target lies within the sorted left side to eliminate the other half.
if nums_array[left_pointer] <= target_value < nums_array[middle_index]:
right_pointer = middle_index - 1
else:
left_pointer = middle_index + 1
else:
# Check if target lies within the sorted right side to eliminate the other half.
if nums_array[middle_index] < target_value <= nums_array[right_pointer]:
left_pointer = middle_index + 1
else:
right_pointer = middle_index - 1
return -1| Case | How to Handle |
|---|---|
| Empty or null input array | Check for null or empty arrays at the start and return negative one immediately. |
| Array with a single element matching or not matching the target | The binary search bounds check the single element directly and return its index or negative one. |
| Array with two elements, either rotated or not | The conditional checks correctly identify which half is sorted even with minimal length. |
| Target is located at the exact pivot point where the rotation occurs | Standard binary search conditions correctly evaluate the middle element against the target regardless of rotation. |
| Array is not actually rotated, meaning the rotation count is zero | The left-sorted check naturally falls through to standard binary search logic when the entire array is ordered. |
| Target is smaller than the minimum or larger than the maximum value in the array | The boundary comparisons fail the range checks for the sorted half and correctly shift the search window. |
| Large scale inputs approaching maximum constraints | The logarithmic time complexity ensures the solution scales efficiently without exceeding time limits. |
| Inputs containing negative numbers and zero values | Using standard integer comparisons handles negative numbers, zeros, and positive numbers uniformly. |