Peak Index in a Mountain Array

Medium

Topics
ArrayBinary Search

An array nums is a mountain: it strictly increases up to a unique peak and then strictly decreases. Return the index of the peak element. Solve in O(log n) time.

Example 1

Input:  nums = [0,1,0]
Output: 1

Example 2

Input:  nums = [0,2,1,0]
Output: 1

Example 3

Input:  nums = [0,10,5,2]
Output: 1

Constraints

  • 3 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^6
  • nums forms a mountain (unique peak).
Run ⌘' · Submit ⌘⏎