visual walkthrough
Find in Mountain Array
Composes three binary searches under an API-call limit.
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
| approach | time | space |
|---|---|---|
| Call get on every index | O(n) | O(1) |
| Peak, then two binary searches | O(log n) | O(1) |