Skip to content

Peak Index in a Mountain Array

MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

A mountain array strictly increases up to a single peak and then strictly decreases. It has at least three elements, and the peak is never at either end. Given a mountain array arr, return the index of the peak in O(log n) time.

arr = [0, 1, 0]        →  1
arr = [0, 2, 1, 0]     →  1
arr = [3, 4, 5, 1]     →  2

Constraints

  • 3 <= arr.length <= 10^5
  • 0 <= arr[i] <= 10^6
  • arr is guaranteed to be a mountain array.