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, return its smallest number, using O(log n) steps.

Example 1
Input: nums = [3, 4, 5, 1, 2]
Output: 1
Explanation: [1, 2, 3, 4, 5] was rotated 3 times.
Example 2
Input: nums = [4, 5, 6, 7, 0, 1, 2]
Output: 0
Explanation: [0, 1, 2, 4, 5, 6, 7] was rotated 4 times.
Example 3
Input: nums = [11, 13, 15, 17]
Output: 11
Explanation: rotated 4 times, the array is back in sorted order.
Solution

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. lo and hi mark the first and last card still in play, and mid marks the card halfway between them. The minimum always stays in play, so the card at hi is always in the low run, and each step compares the card at mid with it.

Binary search on 4, 5, 6, 7, 0, 1, 2. First row: mid is 7, bigger than 2 at hi, so the minimum is right of mid. Second row: mid is 1, smaller than 2, so the minimum is at mid or left of it. Third row: mid is 0, smaller than 1. Last row: lo and hi meet on 0.

In the first row, mid is on 7 and hi on 2. Since 7 > 2, the cards 4 to 7 are ruled out, so in the second row only 0, 1 and 2 are left. There mid is on 1, and 1 < 2, so 1 is in the low run and 2 is ruled out. In the third row 0 < 1, so 1 is ruled out too. In the last row lo and hi meet on one card, 0, the minimum.

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 lo and hi, and when to stop.

Which value to compare with

Why the last value of the range, nums[hi], and not the first, nums[lo]? The first value looks just as natural, but it fails when the array is not rotated at all. In Example 3, [11, 13, 15, 17], every middle value is bigger than the first one, which looks exactly like the high run. A version that compares with nums[lo] moves right and returns 15 there.

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], the middle value is in the high run, so it cannot be the minimum. The range moves past it: 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 = mid, not mid - 1. The numbers are all different, so nums[mid] equals nums[hi] only when mid and hi are the same position.

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 and stops when they meet.

A loop on lo <= hi, as in the usual search for a value, would not end here. With lo and hi equal, mid is that same position, hi = mid changes nothing, and the loop repeats forever.

The middle position is lo + (hi - lo) / 2. It is the same as (lo + hi) / 2, but it stays correct on a huge array. There lo + hi could go past the largest value an int can hold and wrap around to a negative number, while hi - lo never does.

Java Solution

The method findMin works in these steps:

  1. First, set lo to 0 and hi to nums.length - 1, so the range covers the whole array.
  2. While lo < hi, find the middle position mid as lo + (hi - lo) / 2.
  3. Compare nums[mid] with nums[hi]. If it is bigger, set lo = mid + 1. Otherwise set hi = mid.
  4. When lo and hi meet, return nums[lo].

The main method runs the three examples, then a single number, two numbers, an array whose minimum is its last number, and an array with negative numbers.

Java
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:

Output
1
0
11
9
1
1
-9

Look at the third line, 11. In [11, 13, 15, 17] no middle value is bigger than nums[hi], so only hi moves, down to position 0. The fourth line, 9, comes from a one-number array: lo and hi start equal, the loop never runs, and the method returns that number.

The sixth line comes from [2, 3, 4, 5, 1], where the minimum is the very last number. Every step finds a middle value bigger than 1, so only lo moves until it reaches position 4. The last line shows the comparison works the same with negative numbers: the high run is -3, -1, 8 and the method returns -9.

Complexity

All the work is in the loop, and each pass shrinks the range lo to hi to about half.

  • 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, hi and mid.

The position where lo stops is also the number of rotations, except for an array rotated n times, where it is 0. When the array may hold repeated numbers, see Find Minimum in Rotated Sorted Array II.