Problem overview
The method findMin receives an int[] nums of distinct integers. The array was sorted in ascending order and then possibly rotated: its first k elements were moved, in the same order, to the end, with 0 <= k < nums.length. The method returns the smallest element, in O(log n) time and O(1) extra space.
For [3, 4, 5, 1, 2] the answer is 1: the original array was [1, 2, 3, 4, 5], rotated at k = 2. For [4, 5, 6, 7, 0, 1, 2] it is 0. For [11, 13, 15, 17], which was not rotated, it is the first element, 11.
Approach: compare the middle with the last element
Scanning every element finds the minimum in O(n). To get O(log n), use binary search, even though the array is not sorted as a whole. A rotated array rises, drops once at the rotation point, and rises again. The minimum is the element right after the drop, or the first element if there was no rotation.
The decision at each step is a single comparison, nums[middle] against nums[right]:
- If
nums[middle] > nums[right], the values drop somewhere between them. In a sorted stretch every value is smaller than the last one, so a middle value bigger than the last value means the rotation point lies aftermiddle. The minimum is to the right, andmiddleitself cannot be it. - Otherwise
nums[middle] < nums[right](the values are distinct), so everything frommiddletorightis sorted. The minimum is atmiddleor to its left.
The comparison is with the right end, not the left. Comparing with nums[left] fails for an array that was not rotated: nums[middle] is bigger than nums[left], yet the minimum is on the left.
Trace for [4, 5, 6, 7, 0, 1, 2]:
| left | right | middle | nums[middle] | nums[right] | Move |
|---|---|---|---|---|---|
| 0 | 6 | 3 | 7 | 2 | 7 > 2, so left = 4 |
| 4 | 6 | 5 | 1 | 2 | 1 < 2, so right = 5 |
| 4 | 5 | 4 | 0 | 1 | 0 < 1, so right = 4 |
Now left and right are both 4, the loop ends, and nums[4] is 0. For the unrotated [11, 13, 15, 17] every comparison goes to the else branch, and right moves down to 0, so the result is nums[0], which is 11.
The invariant that makes this work is simple: after every pass, the minimum is still somewhere between left and right, inclusive. Both moves keep it inside, and both shrink the window, so the loop must end with a single index left.
Step by step
- Set the window.
left = 0andright = nums.length - 1. The minimum can be anywhere, so the window is the whole array. - Loop while
left < right. It stops when the window is a single index, and that index holds the minimum. This is<, not<=. - Find the middle.
middle = left + (right - left) / 2. Integer division rounds down, somiddleis always less thanrightwhileleft < right. - The key line. If
nums[middle] > nums[right], setleft = middle + 1. The value atmiddleis bigger than another element, so it cannot be the minimum, and it is safe to skip past it. - Otherwise set
right = middle, notmiddle - 1, becausenums[middle]may itself be the minimum. - Return
nums[left]. Every pass kept the minimum inside the window, and the window is now one index.
The solution in Java
class Solution {
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int middle = left + (right - left) / 2;
if (nums[middle] > nums[right]) {
left = middle + 1;
} else {
right = middle;
}
}
return nums[left];
}
}
The Java details worth a closer look:
- The result is a value, not an index. The method returns
nums[left], anint. Returningleftwould give the position of the minimum, which is a different answer. - Integer division.
(right - left) / 2drops the fraction. For a window of two indices,middleisleft. That is whyleft = middle + 1always makes progress, and whyright = middledoes too:middleis strictly less thanrightwheneverleft < right. left + (right - left) / 2against(left + right) / 2. Javaintarithmetic wraps silently on overflow, so adding two large indices can go negative. The subtraction form cannot overflow. Arrays here hold at most a million elements, so either form would work, but the safe one is the better habit.- Declaring two ints at once.
int left = 0, right = nums.length - 1;is one statement.
Complexity
Time: O(log n). Each pass discards at least half of the window, either by moving left past middle or by pulling right back to middle.
Space: O(1). The method uses only left, right and middle.
Edge cases and common mistakes
- One element.
[7]returns 7:leftequalsright, the loop never runs, andnums[0]is returned. - Two elements.
[2, 1]returns 1 and[1, 2]returns 1. For[2, 1],middleis 0, and 2 > 1 movesleftto 1. - No rotation.
[11, 13, 15, 17]returns 11. The array is sorted, so the minimum is the first element. - Comparing with
nums[left]. A version that movesleftwhennums[middle] >= nums[left]returns 17 for[11, 13, 15, 17]and 2 for[4, 5, 6, 7, 0, 1, 2]. Both are wrong. right = middle - 1. This can skip the minimum whenmiddleis the minimum itself, as in the last step of the trace above.- Looping with
left <= right. Withright = middle, a window of one index would never shrink, and the loop would not end. - Duplicates. The method assumes distinct values. With repeats,
nums[middle] == nums[right]would no longer say which side the minimum is on. The statement rules duplicates out.
Testing
[3, 4, 5, 1, 2]gives 1.[4, 5, 6, 7, 0, 1, 2]gives 0.[11, 13, 15, 17]gives 11.[7]gives 7.[2, 1]gives 1.[1, 2]gives 1.
