Solution: Find Minimum in Rotated Sorted Array II
Let’s solve the Find Minimum in Rotated Sorted Array II using the Modified Binary Search pattern.
We'll cover the following
Statement
Imagine you have an array, nums
, of length
, if rotated times , if rotated times
A single rotation moves the last element to the front. So, if the original array is
You are given a sorted, rotated array, nums
, that may include duplicate elements. Your job is to return the minimum element in the array.
Try to solve this problem with the fewest possible operations.
Constraints:
nums.length
nums[i]
nums
is sorted and rotated betweenand times.
Solution
The solution’s essence lies in leveraging a modified binary search to efficiently locate the minimum element in a rotated sorted array, even in the presence of duplicates. The algorithm defines a search boundary using low
and high
pointers, representing the current range where the minimum element could exist. A third pivot
pointer is calculated to point to the middle element of the current search space.
By comparing the element at pivot
with the element at high
, the algorithm determines whether the minimum lies in the left or right half of the current search space and updates the boundaries accordingly. Specifically, it handles the following three cases:
Case 1 (
nums[pivot] < nums[high]
): Thepivot
element lies in the same half as the upper bound. This means the minimum is to the left of or at thepivot
. Therefore, we movehigh
topivot
.Case 2 (
nums[pivot] > nums[high]
): Thepivot
element lies in a different half from the upper bound. Hence, the minimum must lie to the right ofpivot
, and we movelow
next topivot
.Case 3 (
nums[pivot] == nums[high]
): It’s ambiguous which half contains the minimum. To avoid skipping the minimum or falling into an infinite loop, we safely reduce the upper bound (high
) by one. This step ensures progress and correctness without eliminating any possible minimum.
Using the intuition above, we implement the algorithm as follows:
Set two pointers:
low
at the beginning of the array andhigh
at the end. These pointers define the current search space.Iterate through the array as long as
low < high
:Compute the middle index,
pivot
, of the current search space.If the element at
pivot
is less than the element athigh
, the minimum could be atpivot
or anywhere to the left, so shrink the search space by settinghigh = pivot
.On the other hand, if the element at
pivot
is greater than the element athigh
, the minimum must lie to the right ofpivot
. Therefore, update thelow
pointer topivot + 1
.Finally, if both values are equal, we can’t determine which side the minimum is on (due to duplicates). Safely reduce the search space by reducing
high
by one.
When the loop terminates,
low
points to the minimum element. Return the value at indexlow
, representing the minimum element in the rotated sorted array.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.