← Whiteboard questions

Find Minimum in Rotated Sorted Array

Find the smallest number in a sorted array that was rotated

DSA, Medium, Binary Search

Which structure or algorithm, and what does it cost.

Take a list of different whole numbers in increasing order. Move some items from its front to its back, keeping their order. For example, [3, 6, 9, 14] can become [9, 14, 3, 6]. (This only shows the move; the examples are below.) Moving nothing is also allowed. You get the rotated list nums. Return the smallest number in it.

Examples

Input nums = [14, 18, 23, 3, 6, 9]
Output 3
Why The original list was [3, 6, 9, 14, 18, 23]. The order drops from 23 to 3, and 3 is the smallest.
Input nums = [5, 8, 12, 20]
Output 5
Why Nothing was moved, so the first item is the smallest.
Input nums = [7]
Output 7
Why Edge case: a list with one item.

Constraints

Your answer must contain

Target Aim for O(log n) time and O(1) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Look at the last item. Which items in the list are larger than it, and which are smaller or equal?

Hint 2 the approach

The smallest item is where the order drops. That point splits the list into two sorted parts. Which part does a middle item belong to?

Hint 3 the key step

Compare the middle with the last item of the range. One result puts the drop on one side. On the other, can the middle itself be the smallest?

Walkthrough

Full walkthrough brute force to best

Brute force

Scan every item and keep the smallest. That is O(n) time and O(1) memory. It works, but it ignores the sorted order, and with 100,000 items every query reads every item.

Best method

Compare the middle item with the last item of the current range. If the middle is larger, the drop from large to small lies to the right of the middle, so the smallest is right of it: move lo to mid + 1. Otherwise the part from the middle to the end is sorted, so the smallest is at the middle or to its left: move hi to mid and keep the middle, because it may be the answer. Repeat while lo < hi. When one item remains, it is the smallest. This also works when nothing was moved: then no middle is larger than the last item, and the range shrinks to the first item.

Hand trace

Run on [14, 18, 23, 3, 6, 9]. Start with lo 0 and hi 5. The middle is position 2, value 23, which is larger than the last item 9, so lo becomes 3. The middle is position 4, value 6, which is not larger than 9, so hi becomes 4. The middle is position 3, value 3, which is not larger than 6, so hi becomes 3. Now lo and hi meet at position 3, so the answer is 3.

Code

def find_min(nums):
    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 may be the smallest: keep it
    return nums[lo]

Time and memory

O(log n) time, since each step halves the range: about 17 steps for 100,000 items. O(1) memory.

Common mistakes

  • Moving hi to mid - 1 when the middle is not larger than the last item. The middle can be the smallest item, so it must stay in the range.
  • Comparing the middle with the first item of the whole list. On a list that was not moved, such as [5, 8, 12, 20], this sends the search the wrong way.
  • Looping while lo <= hi together with hi = mid. When lo equals hi the range stops shrinking and the loop never ends. Use lo < hi.

Related problems

More Binary Search problems
Find Minimum in Rotated Sorted Array walkthrough