A sorted array of unique integers has been rotated at some unknown pivot. Find the minimum element, in O(log n) time.
Example
Input nums = [4,5,6,7,0,1,2]
Output 0
Key Pointers
Compare the middle value to the rightmost value, not the leftmost — that comparison reliably tells you which half contains the rotation point.
If the middle value is larger than the rightmost value, the minimum must sit somewhere to the right of mid; otherwise it's at mid or somewhere to its left.
The range keeps narrowing until the two pointers land on the same index, which is exactly the minimum.
Step-by-Step Analysis
Set left = 0, right = len(nums) - 1.
While left is below right: compute mid.
If nums[mid] is greater than nums[right], the rotation point lies to the right of mid, so move left to mid + 1.
Otherwise, mid itself could be the minimum, so pull right down to mid.
When left meets right, return nums[left].
Complexity
Time: O(log n) — Space: O(1)
Code
def findMin(nums: list[int]) -> int:
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]
Code Explanation
How the solution above works, piece by piece.
The array is two sorted runs; the minimum is where the second run starts.
Compare nums[mid] with nums[right]. If it's bigger, mid is in the left (higher) run, so the minimum is strictly to the right: left = mid + 1.
Otherwise mid is in the right run, so the minimum is mid or before it: right = mid (keep mid).
Comparing with right rather than left also works when the array isn't rotated at all.
The loop ends when left == right, on the minimum. O(log n).
Visual Walkthrough
Running it on nums = [4, 5, 6, 7, 0, 1, 2]:
Each probe compares the middle with the right end to decide which half contains the drop point. Two probes narrow it to index 4, value 0.