Find Minimum in Rotated Sorted Array

Difficulty: Medium

You're given a rotated sorted list of distinct numbers: sorted smallest to largest, then rotated so it no longer starts at its smallest value (for example [3, 4, 5, 1, 2] instead of [1, 2, 3, 4, 5]).

Find the smallest number in the list.

Examples

Input: nums = [3, 4, 5, 1, 2]
Output: 1

The original sorted order was [1, 2, 3, 4, 5]; it's been rotated so it now starts at 3.

Input: nums = [4, 5, 6, 7, 0, 1, 2]
Output: 0

The smallest value, 0, sits partway through the list.

Input: nums = [11, 13, 15, 17]
Output: 11

This list happens not to look rotated at all, so its smallest value is simply the first element.

Constraints

  • 1 <= nums.length <= 5000

  • -5000 <= nums[i] <= 5000

  • All the integers of nums are unique.

  • nums was originally sorted in ascending order and then rotated between 1 and n times.

Approach

Scanning the list for the smallest value works, but it ignores that the list is two sorted runs glued together.

Binary search still works with a different comparison. Compare the middle element to the last element of the range:

  • If the middle is greater, the rotation point (and the smallest value) is to the right. Drop the middle and everything left of it.
  • Otherwise, the range from the middle on is sorted, so the smallest value is the middle or to its left. Drop the right side.

The range narrows down to the smallest element.

Solutions

Brute Force — Linear Scan

Walk the list, tracking the smallest value so far. Correct, but ignores the sorted structure.

function findMin(nums) {
  let min = nums[0];
  for (let i = 1; i < nums.length; i++) {
    if (nums[i] < min) min = nums[i];
  }
  return min;
}

Time: O(n) · Space: O(1)

Optimal — Binary Search

Narrow a left/right range by comparing the middle element to the range's last element, which tells you which side the smallest value is on.

function findMin(nums) {
  let left = 0;
  let right = nums.length - 1;

  while (left < right) {
    const mid = Math.floor((left + right) / 2);

    if (nums[mid] > nums[right]) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }

  return nums[left];
}

Time: O(log n) · Space: O(1)