Suppose an array of length n
sorted in ascending order is rotated between 1
and n
times. For example, the array nums = [0,1,2,4,5,6,7]
might become:
[4,5,6,7,0,1,2]
if it was rotated 4
times.[0,1,2,4,5,6,7]
if it was rotated 7
times.Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]]
1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]
.
Given the sorted rotated array nums
of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time
.
Example 1:
Input: nums = [3,4,5,1,2] Output: 1 Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Example 2:
Input: nums = [4,5,6,7,0,1,2] Output: 0 Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
Example 3:
Input: nums = [11,13,15,17] Output: 11 Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
Constraints:
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
nums
are unique.nums
is sorted and rotated between 1
and n
times.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:
We want to find the smallest number in a list that was originally sorted but then rotated. A brute force method involves checking each number individually until we find the smallest.
Here's how the algorithm would work step-by-step:
def find_minimum_brute_force(numbers):
# Assume the first element is the minimum initially.
smallest_number_so_far = numbers[0]
for current_number in numbers:
# Compare the current number with the smallest found so far.
if current_number < smallest_number_so_far:
smallest_number_so_far = current_number
return smallest_number_so_far
The core idea is to use a divide-and-conquer strategy similar to how you'd look up a word in a dictionary. We repeatedly narrow down the search area until we isolate the smallest element by cleverly comparing sections of the array.
Here's how the algorithm would work step-by-step:
def find_minimum_in_rotated_sorted_array(numbers):
left_index = 0
right_index = len(numbers) - 1
while left_index < right_index:
middle_index = (left_index + right_index) // 2
# If middle element is greater, min is in right half
if numbers[middle_index] > numbers[right_index]:
left_index = middle_index + 1
# If middle element is smaller, min is in left half (or is middle)
elif numbers[middle_index] < numbers[right_index]:
right_index = middle_index
# Duplicates case, reduce search space
else:
right_index -= 1
# left_index is the index of the minimum element
return numbers[left_index]
Case | How to Handle |
---|---|
Null or empty input array | Return -1 or throw an IllegalArgumentException to indicate invalid input; choose an approach that aligns with requirements or assumptions. |
Array with only one element | Return the single element as it is trivially the minimum. |
Array with two elements, determining the minimum is a simple comparison | Compare the two elements and return the smaller one. |
Array is already sorted (no rotation) | Return the first element as it will be the minimum. |
Large array (performance concerns for linear search) | Use binary search to achieve logarithmic time complexity for efficient performance with large input sizes. |
Array containing negative numbers and zero | Binary search will function correctly as it is based on the relative ordering of elements, regardless of their sign. |
Integer overflow during midpoint calculation in binary search (for extremely large arrays) | Use (low + (high - low) / 2) to avoid potential integer overflow when calculating the midpoint in the binary search. |
The minimum element is the last element in the array after rotation | The binary search logic correctly handles scenarios where the minimum element is located at the array's tail by properly narrowing the search range. |