Skip to content
CodeShare
All problems

Find Minimum in Rotated Sorted Array

Find the smallest value in a sorted array that was rotated.

MediumArraysBinary search

A sorted array of distinct numbers was rotated some number of times: [0, 1, 2, 4, 5, 6, 7] might have become [4, 5, 6, 7, 0, 1, 2].

Given the rotated array nums, return its minimum element in O(log n) time.

Examples

Example 1
Input
nums = [3, 4, 5, 1, 2]
Output
1
Example 2
Input
nums = [4, 5, 6, 7, 0, 1, 2]
Output
0
Example 3
Input
nums = [11, 13, 15, 17]
Output
11
Why
The array was rotated a full cycle, so it's still sorted.

Constraints

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