The brute-force solution checks every element in the array, ignoring the sorted and rotated properties, leading to:
The optimal solution uses binary search to find the minimum in O(log n) time complexity:
The “Find Minimum in Rotated Sorted Array” problem involves identifying the smallest element in an array that was initially sorted in ascending order but then rotated at some pivot unknown to you beforehand. The array contains no duplicate elements and is guaranteed to have been rotated at least once.
Example:
Input: nums = [4,5,6,7,0,1,2]
→ Output: 0
Input: nums = [3,4,5,1,2]
→ Output: 1
This problem is a classic application of binary search under modified conditions. It teaches you to recognize how the sorted property can still be leveraged in rotated arrays to achieve logarithmic time complexity. Such problems are highly relevant in technical interviews and real-world scenarios involving circular buffers, search optimization, and time series analysis.
The goal is to find the index of the smallest element using binary search in O(log n)
time. Since the array is sorted and rotated, the minimum element is the only "pivot point" where the order breaks.
Comparing the middle element with the rightmost element helps decide which side the minimum lies on.
left = 0
and right = nums.length - 1
.left < right
:
mid = Math.floor((left + right) / 2)
.nums[mid] > nums[right]
, the minimum must be in the right half → set left = mid + 1
.mid
) → set right = mid
.left
will point to the minimum element.nums[left]
.
Input: nums = [4,5,6,7,0,1,2]
Time Complexity: O(log n), where n is the number of elements in the array.
Space Complexity: O(1), since only constant space is used.
The “Find Minimum in Rotated Sorted Array” problem is a smart application of binary search that demonstrates how to adapt core techniques to new scenarios. Recognizing patterns and utilizing comparisons between midpoints and boundaries allows for an efficient and elegant solution.