Start learning free

LESSON 100 OF 314 · BINARY SEARCH

Find Minimum in Rotated Sorted Array in Go: O(log n) Binary Search

Practice O(log n) Read the full explanation

You get an array nums of different integers.

Return the smallest number in nums.

The numbers were sorted from smallest to largest. Then the array may have been rotated. A rotation moves the first k numbers to the end, in the same order. Here 0 <= k < len(nums).

For example, [1,4,6,9] with k = 1 becomes [4,6,9,1]. With k = 0, nothing moves.

 

Example 1:

Input: nums = [21,30,4,9,16]
Output: 4
Explanation: The sorted array was rotated with k = 3.
So 4, 9 and 16 moved to the end.
The smallest number is 4.

Example 2:

Input: nums = [-6,-2,5,12]
Output: -6
Explanation: This array was not rotated (k = 0).
The first number is the smallest.

Example 3:

Input: nums = [40,-5]
Output: -5

 

Constraints:

  • 1 to 1,000,000 numbers.
  • All numbers are different.
  • Each number is from -109 to 109.
  • The array is sorted, then maybe rotated (see above).

Note:

Your algorithm should run in O(log n) time and use O(1) extra space.

STEP 1 OF 6
    Ctrl + Enter to check

    GO · BINARY SEARCH

    Find Minimum In Rotated Sorted Array in Go, explained

    Problem overview

    The function findMin receives a slice of distinct integers, nums, that was sorted in ascending order and then possibly rotated: its first k elements were moved, in the same order, to the end. It returns the smallest element. For example, [0, 1, 2, 4, 5, 6, 7] rotated at k = 3 becomes [4, 5, 6, 7, 0, 1, 2], and the answer is 0. With k = 0 the slice is unchanged, so [11, 13, 15, 17] gives 11.

    The budget is O(log n) time and O(1) extra space, so scanning the whole slice for the minimum is too slow, and the sorted structure has to be used.

    Approach: compare the middle with the last element

    The minimum sits at the rotation point, the one place where the values drop. In [4, 5, 6, 7, 0, 1, 2] it is the 0 right after the 7. Binary search can find that point if each comparison tells which side of middle the drop is on.

    Compare nums[middle] with nums[right], the last element of the current window:

    • If nums[middle] > nums[right], the values drop somewhere after middle, since in a sorted stretch every element is smaller than the last one. The minimum is to the right of middle, and middle itself can't be it: skip it with left = middle + 1.
    • Otherwise nums[middle] is smaller than nums[right], and everything from middle to right is sorted. The minimum is at middle or to its left, so the window shrinks to right = middle. It isn't middle - 1, because nums[middle] may be the minimum itself.

    The loop runs while left < right, and ends when the window is one index wide. That index holds the minimum.

    The trace for [4, 5, 6, 7, 0, 1, 2]:

    left 0, right 6, middle 3: 7 > 2, so left = 4
    left 4, right 6, middle 5: 1 > 2 is false, so right = 5
    left 4, right 5, middle 4: 0 > 1 is false, so right = 4
    left = right = 4: the minimum is nums[4] = 0

    Why compare with nums[right] and not nums[left]? With [11, 13, 15, 17], nums[middle] is bigger than nums[left], just as in a rotated slice, yet the minimum is on the left. The last element has no such ambiguity.

    Step by step

    1. Declare both ends in one statement: left is 0 and right is len(nums) - 1. The minimum can be at any index, so the window starts as the whole slice.
    2. Loop while left < right. Unlike a search for a target, this loop stops at one index, and that index is the answer.
    3. Compute middle as left + (right-left)/2, which rounds down.
    4. If nums[middle] > nums[right], the minimum is to the right of middle: set left to middle + 1.
    5. Otherwise, everything from middle to right is sorted, so the minimum is at middle or to its left: set right to middle.
    6. Return nums[left]. Each pass kept the minimum inside the window, and the window is now a single index.

    The solution in Go

    func findMin(nums []int) int {
        left, right := 0, len(nums)-1
        for left < right {
            middle := left + (right-left)/2
            if nums[middle] > nums[right] {
                left = middle + 1
            } else {
                right = middle
            }
        }
        return nums[left]
    }
    

    The Go details worth a closer look:

    • Go's while loop. for left < right { ... } is a for with only a condition. Go has no while keyword.
    • Multiple declaration. left, right := 0, len(nums)-1 declares both variables in one statement.
    • Rounding down matters here. Because (right-left)/2 rounds down, middle is always less than right while left < right. That guarantees right = middle shrinks the window. If middle could equal right, the loop could stay on the same window forever.
    • The function returns a value, not an index. nums[left] is the element itself.
    • An empty slice panics. len(nums)-1 would be -1 and nums[left] would be out of range. The constraints guarantee at least one element.

    Complexity

    Time: O(log n). Each pass discards at least half of the window: left = middle + 1 drops the left half and the middle, and right = middle drops the right half. So about log₂ n passes run.

    Space: O(1). Only left, right and middle are kept, and the slice is not changed.

    Edge cases and common mistakes

    • One element. [1] gives 1. The loop condition is false at once, and nums[0] is returned.
    • Not rotated. [11, 13, 15, 17] gives 11. The middle is never bigger than the last element, so right keeps moving left until it reaches index 0.
    • Two elements. [2, 1] gives 1 and [1, 2] gives 1. The middle is index 0, which is compared with nums[1].
    • Minimum at the last index. [2, 3, 4, 5, 1] gives 1. The window keeps moving right until it reaches the last index.
    • Setting right = middle - 1. This can skip the minimum when nums[middle] is the minimum itself. For [4, 5, 1, 2, 3] the middle is the 1 on the first pass, and that version returns 4.
    • Comparing with nums[left]. A version that moves left up whenever nums[middle] > nums[left] returns 5 for [5, 1, 2, 3, 4] and 15 for [11, 13, 15, 17], both wrong.
    • Duplicates. The lesson guarantees distinct values. With repeated values, nums[middle] equal to nums[right] doesn't say which side holds the minimum.

    Testing

    • [3, 4, 5, 1, 2] gives 1.
    • [4, 5, 6, 7, 0, 1, 2] gives 0.
    • [11, 13, 15, 17] gives 11.
    • [1] gives 1.
    • [2, 1] gives 1.
    • [5, 1, 2, 3, 4] gives 1.
    • [2, 3, 4, 5, 1] gives 1.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free