AceYourInterviews
Checking sign-in…

63. Minimum in Rotated Sorted Array

CoreRetain the minimum using the right boundary

An increasing sorted array has been rotated by moving a prefix to the end. Find its smallest value using the remaining order structure.

Return the value, not its position. The array is nonempty and has distinct values; preserve it and use logarithmic search.

Example 1

Input

[4,5,6,7,0,1,2]

Output

0

Explanation: nums[mid]=7 exceeds right value 2, so the minimum is strictly after mid.

Example 2

Input

[1,2,3]

Output

1

Explanation: An already sorted interval keeps its left side, eventually leaving index 0.

Example 3

Input

[2,3,4,1]

Output

1

Explanation: When mid is larger than the right endpoint, discard through mid; the final candidate is the last index.

Constraints

  • 1 <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1
  • nums is a rotation of a strictly increasing array; all values are distinct.
Hints
Hint 1

The minimum must stay in one nonempty closed interval.

Hint 2

Compare nums[mid] to the current nums[right], not to a target.

Hint 3

If mid is higher, move left past it; otherwise retain it with right=mid.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Midpoint comparison finds a late pivot

[2,3,4,1]
1