153. Find Minimum in Rotated Sorted Array#
Difficulty: Medium
Link to Problem: To see the Find Minimum in Rotated Sorted Array problem on LeetCode, click here!
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 rotated4times.[0,1,2,4,5,6,7]if it was rotated7times.
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.
Constraints:
n == nums.length1 <= n <= 5000-5000 <= nums[i] <= 5000All the integers of
numsare unique.numsis sorted and rotated between1andntimes.
def findMin(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) // 2
# If the mid element is greater than the rightmost element,
# it means the minimum element is in the right half.
if nums[mid] > nums[right]:
left = mid + 1
# If the mid element is less than or equal to the rightmost element,
# it means the minimum element is in the left half or at the mid itself.
else:
right = mid
# The loop will break when left and right converge to the minimum element.
# At this point, left (or right) will be pointing to the minimum element.
return nums[left]
Explanation:#
The function
findMin(nums)takes an input listnums, which represents the rotated sorted array.Initialize two pointers
leftandrightto track the current search range. Initially,leftis set to 0 (the beginning of the array), andrightis set tolen(nums) - 1(the end of the array).Enter a
whileloop with the conditionleft < right. This loop continues untilleftandrightconverge to a single element, which will be the minimum element in the rotated array.Inside the loop, calculate the middle index
midusing integer division. This helps in finding the middle element of the current search range.Check if the element at the middle index (
nums[mid]) is greater than the element at the rightmost index (nums[right]). If this condition is true, it means that the minimum element must be in the right half of the current search range. So, updatelefttomid + 1, effectively eliminating the left half of the search range.If the condition from step 5 is not met, it implies that the minimum element can be in the left half of the current search range or could be the element at the
midindex itself. In this case, updaterighttomid, effectively eliminating the right half of the search range.Repeat steps 4-6 until
leftandrightconverge to the minimum element.When the loop exits, it means that
left(orright) points to the minimum element in the array.Return
nums[left](ornums[right]) as the result, which is the minimum element in the rotated sorted array.
Test cases#
### Example 1:
nums1 = [3, 4, 5, 1, 2]
print(findMin(nums1))
1
# Example 2:
nums2 = [4, 5, 6, 7, 0, 1, 2]
print(findMin(nums2))
0
# Example 3:
nums3 = [11, 13, 15, 17]
print(findMin(nums3))
11
Let’s analyze the time and space complexity of the provided code:
Time Complexity:
The time complexity of this code is \(O(log\ n)\), where ‘\(n\)’ is the length of the input array nums. This is because the binary search algorithm is employed, which continually divides the search range in half with each iteration.
In each iteration of the while loop, the search range is halved, and this process continues until the left and right pointers converge to the minimum element. In the worst case, it will take logarithmic time to reduce the search range to a single element.
Therefore, the binary search used in this code runs in \(O(log\ n)\) time complexity.
Space Complexity: The space complexity of this code is \(O(1)\), which means it uses a constant amount of additional space that does not depend on the size of the input array.
The algorithm uses a fixed number of variables (left, right, mid) to keep track of the search range and indices, but the number of these variables does not grow with the size of the input array. Hence, the space complexity is constant.
In summary:
Time Complexity: \(O(log\ n)\)
Space Complexity: \(O(1)\)
Challenging Exercises:#
Solve the problem with a variation where the input array may contain duplicates.
Modify the problem to return the index of the minimum element in the rotated sorted array, rather than the element itself. The algorithm should still run in \(O(log\ n)\) time.
Implement a function that finds the maximum element in a rotated sorted array. How would you adapt the binary search algorithm to solve this problem efficiently in \(O(log\ n)\) time?
Implement a function that can find the kth smallest element in a rotated sorted array. This is an extension of the original problem, and the algorithm should still run in \(O(log\ n)\) time.