Skip to main content
Back to problems
#2951
Easy Algorithms

Find the peaks

Array Enumeration
75.0% acceptance
Feb 25, 2026
216
18
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn find_peaks(mountain: Vec<i32>) -> Vec<i32> {
    (1..mountain.len() - 1)
      .filter(|&i| mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1])
      .map(|i| i as i32)
      .collect()
  }
}