Loading problems...
TL;DR: Use a modified binary search to locate the "pivot" point where the array order resets, comparing the middle element against the right boundary to determine which half contains the minimum.
The problem asks us to find the minimum element in an array of unique integers that was originally sorted in ascending order but has been rotated between 1 and n times. For example, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. We must return the smallest value. This is a classic interview question, and the optimal LeetCode 153 solution requires an algorithm with logarithmic time complexity.
The most straightforward way to solve "Find Minimum in Rotated Sorted Array" is to iterate through the entire array and track the minimum value found so far.
Naive Algorithm:
min_val to infinity or the first element of the array.min_val, update .min_valmin_val.Pseudo-code:
textfunction findMin(nums): min_val = nums[0] for x in nums: if x < min_val: min_val = x return min_val
Time Complexity Analysis: The time complexity is O(N), where N is the length of the array, because we must inspect every element once.
Why it fails: While this approach produces the correct result, the problem explicitly mandates an O(log n) time complexity. A linear scan is too slow to satisfy the constraints of the optimal solution, particularly for large datasets where binary search is expected.
The key intuition for solving LeetCode 153 lies in understanding the structure of a rotated sorted array. A sorted array that has been rotated consists of two sorted subarrays:
[4, 5, 6, 7]).[0, 1, 2]).The minimum element is the first element of the Right Sorted Portion. It is the only element in the array that is smaller than its predecessor (if a predecessor exists).
To find this element in O(log n) time, we cannot simply look for a specific target value. Instead, we use the sorted property to determine which half of the array contains the minimum.
The Comparison Logic:
We compare the middle element (nums[mid]) with the rightmost element of the current search space (nums[right]).
If nums[mid] > nums[right]:
mid belongs to the Left Sorted Portion (the values that were rotated to the front).mid.left to mid are all greater than nums[right], so the drop-off (pivot) happens after mid.If nums[mid] < nums[right]:
[mid, right] is sorted.mid is either the minimum itself or the minimum is to the left of mid.mid because nums[mid] is already smaller than nums[right].This logic allows us to discard half of the array reliably in every step, maintaining the invariant that the minimum element always exists within our current [left, right] bounds.

Let's trace the algorithm with the input nums = [4, 5, 6, 7, 0, 1, 2].
Initial State:
left = 0, right = 6.[4, 5, 6, 7, 0, 1, 2].Iteration 1:
mid = 0 + (6 - 0) / 2 = 3.nums[mid] is 7. nums[right] is 2.7 > 2 is True.[4, 5, 6, 7] consists of large values. The minimum is to the right.left = mid + 1 = 4.Iteration 2:
[4, 6]. Values: [0, 1, 2].mid = 4 + (6 - 4) / 2 = 5.nums[mid] is 1. nums[right] is 2.1 > 2 is False.nums[mid] < nums[right] means the range [mid, right] is sorted. The minimum is at mid or to its left.right = mid = 5.Iteration 3:
[4, 5]. Values: [0, 1].mid = 4 + (5 - 4) / 2 = 4.nums[mid] is 0. nums[right] (index 5) is 1.0 > 1 is False.nums[mid] < nums[right].right = mid = 4.Termination:
left (4) is no longer less than right (4).nums[4], which is 0.The correctness relies on the invariant that the minimum element is always within the search range [left, right].
nums[mid] > nums[right], the pivot (minimum) must be in the index range (mid, right]. By setting left = mid + 1, we preserve the minimum in the new range.nums[mid] < nums[right], the sequence from mid to right is ascending. The minimum cannot be in (mid, right]. Thus, it must be in [left, mid]. By setting right = mid, we preserve the minimum in the new range.mid < right is guaranteed by the floor division). Eventually, left == right, pointing to the single remaining candidate, which must be the minimum.cppclass Solution { public: int findMin(vector<int>& nums) { int left = 0; int right = nums.size() - 1; // Loop until the search space converges to a single element while (left < right) { int mid = left + (right - left) / 2; // If mid element is greater than right element, // the minimum is in the right half (excluding mid) if (nums[mid] > nums[right]) { left = mid + 1; } // If mid element is less than right element, // the minimum is in the left half (including mid) else { right = mid; } } // When left == right, we have found the minimum return nums[left]; } };
javaclass Solution { public int findMin(int[] nums) { int left = 0; int right = nums.length - 1; // Loop until pointers meet while (left < right) { int mid = left + (right - left) / 2; // Compare mid with the right boundary if (nums[mid] > nums[right]) { // Minimum is to the right left = mid + 1; } else { // Minimum is at mid or to the left right = mid; } } return nums[left]; } }
pythonclass Solution: def findMin(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left < right: mid = left + (right - left) // 2 # If the middle element is greater than the rightmost element, # the minimum value must be in the right portion. if nums[mid] > nums[right]: left = mid + 1 else: # Otherwise, the minimum is at mid or in the left portion. right = mid return nums[left]
N, the algorithm performs roughly comparisons.left, right, and mid pointers. No auxiliary data structures are used.The Binary Search - Find Min/Max in Rotated Sorted Array pattern is versatile. Understanding how to use the array's structure to guide the binary search steps is applicable to several other problems:
nums[mid] == nums[right].Q: Why do I get a Time Limit Exceeded (TLE) error?
while left <= right without breaking correctly or adjusting right properly.right = mid inside a while left <= right loop, the loop may never terminate when left == mid.while left < right to converge on a single element.Q: Why can't I compare nums[mid] with nums[left]?
nums[mid] with to decide the direction.nums[left]nums[mid] > nums[left] is true, but the minimum is at left. In a rotated array, nums[mid] > nums[left] means the left side is sorted, so the minimum is to the right. This ambiguity requires extra logic to handle the "fully sorted" case.nums[right] is cleaner because nums[mid] < nums[right] always means the right side is sorted, regardless of rotation.Q: Why do I return the wrong value when the array is not rotated?
nums is [1, 2, 3].nums[i] > nums[i+1]). If the array is fully sorted, this drop never exists.right to mid until it hits index 0).