Skip to content

visual walkthrough

Find in Mountain Array

Composes three binary searches under an API-call limit.

Solve on LeetCode

The idea

A mountain array is two sorted halves glued at the peak: rising, then falling.

Find the peak with a slope search, then binary search the rising half (it has the smaller indices) and, only if needed, the falling half with the comparisons flipped.

Complexity

approachtimespace
Call get on every indexO(n)O(1)
Peak, then two binary searchesO(log n)O(1)

More walkthroughs