Find the Peaks - Problem

You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.

Return an array that consists of indices of peaks in the given array in any order.

Notes:

  • A peak is defined as an element that is strictly greater than its neighboring elements.
  • The first and last elements of the array are not a peak.

Input & Output

Example 1 — Basic Case
$ Input: mountain = [2,4,6,3,1]
Output: [2]
💡 Note: Element at index 2 (value 6) is greater than both neighbors: 6 > 4 and 6 > 3
Example 2 — Multiple Peaks
$ Input: mountain = [1,4,3,8,5]
Output: [1,3]
💡 Note: Index 1: 4 > 1 and 4 > 3. Index 3: 8 > 3 and 8 > 5
Example 3 — No Peaks
$ Input: mountain = [1,2,3]
Output: []
💡 Note: Only one interior element at index 1: 2 > 1 but 2 < 3, so not a peak

Constraints

  • 3 ≤ mountain.length ≤ 1000
  • 1 ≤ mountain[i] ≤ 104

Visualization

Tap to expand
Find the Peaks ProblemInput: mountain = [2,4,6,3,1]2463101234Skip first4 > 2 ✓4 > 6 ✗6 > 4 ✓6 > 3 ✓3 > 6 ✗Skip lastPeak found at index 2: element 6 is greater than both neighborsOutput: [2]
Understanding the Visualization
1
Input Array
Array [2,4,6,3,1] with indices 0-4
2
Peak Check
Check each interior element against neighbors
3
Result
Return indices of all peaks found
Key Takeaway
🎯 Key Insight: A peak must be strictly greater than both left and right neighbors, and boundary elements cannot be peaks
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen