Find Minimum in Rotated Sorted Array

MediumBinary Search

An array of distinct integers was sorted in ascending order and then rotated by some unknown number of positions: the first part was cut off and moved to the end. For example [0, 1, 2, 4, 5, 6, 7] rotated by 3 becomes [4, 5, 6, 7, 0, 1, 2]. A rotation by 0 (or by a multiple of the length) leaves the array sorted.

Given the rotated array nums, return its smallest element. Your solution must run in O(log n) time.

Constraints

  • 1 <= nums.length <= 5000
  • -5000 <= nums[i] <= 5000
  • All values are distinct.

Examples

Example 1

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

The array drops from 7 to 0, so the minimum is 0.

Example 2

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

Rotated by 3: the second run starts at 1.

Example 3

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

Rotating by 0 (or by a full lap) leaves the array sorted, so the minimum is the first element.

JavaScript you'll use: Arrays: create, read, update Numbers, Math and integer tricks

/**
 * @param {number[]} nums
 * @return {number}
 */
function findMin(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.