Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find a peak in a mountain array using binary search

Locate the peak index in a mountain (bitonic) array in O(log n) time.

Python practice16 minSearching & SortingIntermediateLast updated March 25, 2026

Problem statement

A mountain array (also called bitonic) is an array where elements strictly increase to a single peak, then strictly decrease. Given such an array, return the index of the peak element. Use an algorithm with O(log n) time complexity.

Task

Apply binary search variant to find the maximum (peak) in an array that strictly increases then strictly decreases.

Examples

Simple mountain

Input

arr = [1,2,3,4,3,2,1]

Output

3

The peak value 4 is at index 3.

Input format

One argument: arr (list of integers) with length >= 3 and a valid mountain shape.

Output format

An integer index of the peak element.

Constraints

Time complexity: O(log n). Array length n >= 3. The array strictly increases then strictly decreases with exactly one peak.

Samples

Sample 1

Input

arr = [0,2,1,0]

Output

1

2 is the peak at index 1.