DSA and coding Level 2 This level · 8 of 30
Medium dsa binary-search

Find Minimum in Rotated Sorted Array

The 30 second answer

A rotated sorted array of unique values is two increasing runs, and the minimum is the start of the second run, or the first element if the array was not rotated. I compare mid with the right end: if mid is greater than the right, the minimum is to the right of mid; otherwise mid itself may be the minimum and I search to the left.

Could you say this in an interview?

Problem You are given an array that was sorted in ascending order and then rotated some number of times, with no duplicates. Return the minimum element in O(log n) time. Original: LeetCode 153: Find Minimum in Rotated Sorted Array.

Understanding the prompt Rotation means some prefix was moved to the end, for example [0,1,2,4,5,6,7] becoming [4,5,6,7,0,1,2]. The minimum is the only place a value is smaller than its predecessor, or index 0 when there is no drop. Unique values keep the two sides of mid comparable without ties.

Pattern recognition Boundary binary search for the first index where the value is less than or equal to the rightmost value of the current range.

Brute force A linear scan, or sort and take the first element, both of which miss the logarithmic brief.

Optimal lo = 0, hi = n - 1. While lo < hi, mid = lo + (hi - lo) / 2. If nums[mid] > nums[hi], the drop is to the right of mid, so lo = mid + 1. Otherwise the minimum is at mid or to its left, so hi = mid, not mid - 1, because mid might be the minimum. When the loop ends, nums[lo] is the answer.

Alternatives Find the rotation index and return that value; it is the same comparison. Search in Rotated Sorted Array builds on this picture but also has to locate a target.

Java solution

public class FindMinimumInRotatedSortedArray {
    static int findMin(int[] nums) {
        int lo = 0, hi = nums.length - 1;
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (nums[mid] > nums[hi]) lo = mid + 1;
            else hi = mid;
        }
        return nums[lo];
    }

    public static void main(String[] args) {
        System.out.println(findMin(new int[] {3, 4, 5, 1, 2}));       // 1
        System.out.println(findMin(new int[] {4, 5, 6, 7, 0, 1, 2})); // 0
        System.out.println(findMin(new int[] {11, 13, 15, 17}));      // 11
    }
}

Complexity O(log n) time, O(1) extra space.

Edge cases

  • Official samples: [3,4,5,1,2] returns 1; [4,5,6,7,0,1,2] returns 0; [11,13,15,17] is not rotated and returns 11.
  • A two-element rotation such as [2,1] returns 1.

Follow-ups

  • With duplicates (LeetCode 154), nums[mid] == nums[hi] can force you to decrement hi by one, and the worst case becomes linear.
  • Search in Rotated Sorted Array asks for a target, not the minimum.

Tests [3,4,5,1,2] returns 1. [4,5,6,7,0,1,2] returns 0. [11,13,15,17] returns 11.

How to say it in the interview "A rotated sorted array is two increasing runs, and the minimum starts the second run. I compare mid with the right end: if mid is larger, the minimum is to the right; otherwise mid might be the minimum and I search left, keeping mid in range. Logarithmic time."

Sources (1)
  1. LeetCode 153: Find Minimum in Rotated Sorted Arrayleetcode.com
Previous
Next