MediumArrayBinary Search

Peak Index in a Mountain Array

LeetCode
2 approaches, code in all languages

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^5
  • 0 ≤ arr[i] ≤ 10^6
  • arr is guaranteed to be a valid mountain array
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now