Peak Index in a Mountain Array
2 approaches, code in every language
Problem description
A mountain array is one that strictly climbs up to a single summit and then strictly slides back down. Given such an array, report the index of that summit, the position where the values stop increasing and begin decreasing.
Since the shape guarantees exactly one peak and no flat stretches, you can steer toward it by checking the slope at the midpoint rather than examining every entry.
Example 1
Input: arr = [0,2,1,0]
Output: 1
Values rise to 2 at index 1 and fall afterward, so the peak index is 1.
Example 2
Input: arr = [0,10,5,2]
Output: 1
The largest value 10 sits at index 1, which is the summit.
Constraints
3 ≤ arr.length ≤ 10^50 ≤ arr[i] ≤ 10^6arr is guaranteed to be a valid mountain array
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Kiln Ramp Peak is our own problem on the technique behind Peak Index in a Mountain Array: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free