Back to Binary Search
Binary Search
Medium

Peak Index in a Mountain Array

LAB

Given a mountain array, return the index of the peak value.

EXAMPLES

Example 1
Input
{
  "arr": [
    0,
    2,
    1,
    0
  ]
}

Output
1

FUNCTION SHAPE

arr: intArrayint
SOLUTION NOTE

Same peak-finding template as Find Peak Element, but the mountain-array guarantee means the peak is unique.

Reveal reference solution +
pythonREFERENCE
def peakIndexInMountainArray(self, arr: List[int]) -> int:
    left, right = 0, len(arr) - 1
    while left < right:
        mid = ceil(left + (right - left) / 2)
        if arr[mid - 1] <= arr[mid]:
            left = mid
        else:
            right = mid - 1
    return left
TimeO(log n)
SpaceO(1)
Open on LeetCode
00:00
3 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.