Find Minimum in Rotated Sorted Array

Pattern: Binary search on rotated arrays

medium

The minimum sits just after the single fall. Compare nums[mid] with nums[hi]. If mid is bigger, the fall is to the right; otherwise mid..hi is sorted and the minimum is at mid or before it.

The problem

A sorted array of different whole numbers was rotated between 1 and n times, so it might be unchanged. Return its smallest value in O(log n) time. For example, [3, 4, 5, 1, 2] gives 1, [4, 5, 6, 7, 0, 1, 2] gives 0, and [11, 13, 15, 17] gives 11.

The function name findMin matches LeetCode. Use the link above for the exact statement and limits.

Think first

  • Where does the minimum sit relative to the one fall in the array?
  • What does nums[mid] > nums[hi] tell you about where the fall is?
  • And nums[mid] < nums[hi]?

Hints

    Brute force, and why it fails

    Scan for the smallest value: O(n). Binary search on the fall: O(log n).

    The key insight

    Comparing with the right end answers one clear question: is the fall to the right of mid? That is a no/yes test, so the lo < hi template finds the first value after the fall.

    Dry run

    40lo
    51 
    62 
    73 
    04 
    15 
    26hi

    nums = [4, 5, 6, 7, 0, 1, 2]. The minimum is the 0 right after the fall.

    Step 1 of 4

    The plan

    lo = 0, hi = n - 1
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if nums[mid] > nums[hi]: lo = mid + 1
        else: hi = mid
    return nums[lo]
    

    Complexity

    Time
    O(log n)
    Space
    O(1)

    Each step halves the range.

    Edge cases

    • Not rotated: nums[mid] is never above nums[hi], so hi walks down to 0.
    • Two values, [2, 1]: mid = 0 holds 2, above 1, so the answer is 1.
    • Rotated by one, as in [5, 1, 2, 3, 4]: the minimum is at index 1.
    I've built my logic, show the code

    Java

    class Solution {
        public 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;   // the drop is to the right of mid
                else hi = mid;                            // mid..hi is sorted, so the minimum is at mid or left
            }
            return nums[lo];
        }
    }

    C++

    class Solution {
    public:
        int findMin(vector<int>& nums) {
            int lo = 0, hi = static_cast<int>(nums.size()) - 1;
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (nums[mid] > nums[hi]) lo = mid + 1;   // the drop is to the right of mid
                else hi = mid;                            // mid..hi is sorted, so the minimum is at mid or left
            }
            return nums[lo];
        }
    };

    Python

    class Solution:
        def findMin(self, nums: list[int]) -> int:
            lo, hi = 0, len(nums) - 1
            while lo < hi:
                mid = (lo + hi) // 2
                if nums[mid] > nums[hi]:
                    lo = mid + 1                    # the drop is to the right of mid
                else:
                    hi = mid                        # mid..hi is sorted, so the minimum is at mid or left
            return nums[lo]

    Common mistakes

    • Comparing with nums[lo], which cannot tell "not rotated" from "fall on the right".
    • Setting hi = mid - 1, which can skip the minimum when it is at mid.
    • Looping while lo ≤ hi with hi = mid, which never ends.

    Similar problems