Find Minimum in Rotated Sorted Array
Find the smallest value in a sorted array that was rotated, by halving the search range at each step.
An array of different integers was sorted in ascending order and then rotated: one rotation moves the last number to the front. It was rotated between 1 and n times, where n is its length. Given the rotated array nums
Example 1
Output: 1
Explanation: [1, 2, 3, 4, 5] was rotated 3 times.
Example 2
Output: 0
Explanation: [0, 1, 2, 4, 5, 6, 7] was rotated 4 times.
Example 3
Output: 11
Explanation: rotated 4 times, the array is back in sorted order.
The Idea
Think of a row of numbered cards laid out from smallest to largest: 0, 1, 2, 4, 5, 6, 7. Take the last card and put it at the front, and do that four times. The row now reads 4, 5, 6, 7, 0, 1, 2, which is Example 2.
The row still climbs, but in two pieces. It climbs from 4 to 7, drops to 0, and climbs again from 0 to 2. Call the first piece the high run and the second the low run. The smallest number, called the minimum, is the first card of the low run, right where the row drops.
Reading every card until the drop would find it, but that can take all n steps. The problem asks for O(log n) steps, which means the number of steps may only grow with how many times n can be halved. That is about 20 steps for a million cards. So we can only look at a few cards. Can one card tell us which side of the drop it is on?
It can, if we compare it with the last card. The last card, 2, is in the low run. Every card in the high run is bigger than every card in the low run, because the high run holds the largest numbers of the sorted row. So a card bigger than the last card is in the high run, and the smallest card is somewhere to its right. A card that is not bigger is in the low run, and the smallest card is that card or to its left.
So one look at the card in the middle rules out about half of the cards. Look at the picture below. lohimidhimid
In the first row, midhimidlohi
Looking at the middle of a range and keeping only the half that can hold the answer is called binary search. Usually it searches a sorted array for a value. Here the array is not fully sorted, but comparing with the last card in play still says which half to keep.
Solution Approach
Turning that idea into code means settling three things: which value to compare the middle with, how to move lohi
Which value to compare with
Why the last value of the range, nums[hi]nums[lo][11, 13, 15, 17]nums[lo]
The same trouble comes up inside a rotated array. Once the range lies inside the low run, its first value is its smallest, so every middle value looks bigger and the search again moves right. For Example 2 that version returns 2.
The last value has no such problem. When the range is a single climb, the last value is its largest, so every middle value is smaller and the search moves left, toward the smallest value.
Moving lo and hi
What happens to the range after a comparison? If nums[mid] > nums[hi]lo = mid + 1
Otherwise the middle value is in the low run, and it may be the minimum itself, as 0 is in the third row of the picture. So the range keeps it: hi = midmid - 1nums[mid]nums[hi]midhi
When to stop
How do we know the search is done? Each step keeps the minimum inside the range, so once the range holds a single value, that value is the answer. The loop runs while lo < hi
A loop on lo <= hilohimidhi = mid
The middle position is lo + (hi - lo) / 2(lo + hi) / 2lo + hiinthi - lo
Java Solution
The method findMin
- First, set
to 0 andlo tohi , so the range covers the whole array.nums.length - 1 - While
, find the middle positionlo < hi asmid .lo + (hi - lo) / 2 - Compare
withnums[mid] . If it is bigger, setnums[hi] . Otherwise setlo = mid + 1 .hi = mid - When
andlo meet, returnhi .nums[lo]
The main
public class FindMinimumInRotatedSortedArray {
public static int findMin(int[] nums) {
int lo = 0;
int hi = nums.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) {
lo = mid + 1; // mid is in the high run: the minimum is to its right
} else {
hi = mid; // mid is in the low run: the minimum is at mid or left
}
}
return nums[lo];
}
public static void main(String[] args) {
System.out.println(findMin(new int[] {3, 4, 5, 1, 2}));
System.out.println(findMin(new int[] {4, 5, 6, 7, 0, 1, 2}));
System.out.println(findMin(new int[] {11, 13, 15, 17}));
System.out.println(findMin(new int[] {9}));
System.out.println(findMin(new int[] {2, 1}));
System.out.println(findMin(new int[] {2, 3, 4, 5, 1}));
System.out.println(findMin(new int[] {-3, -1, 8, -9, -7}));
}
}
Running it prints:
1
0
11
9
1
1
-9
Look at the third line, 11. In [11, 13, 15, 17]nums[hi]hilohi
The sixth line comes from [2, 3, 4, 5, 1]lo
Complexity
All the work is in the loop, and each pass shrinks the range lohi
- Time: O(log n), because the range starts at n numbers and each comparison keeps about half of it, so it reaches a single number after about log2(n) passes.
- Space: O(1), because the method reads the array where it is and keeps only
,lo andhi .mid
The position where lo