LeetCode Problem Workspace

Find the Peaks

Identify all peaks in a mountain array using direct array enumeration and strict neighbor comparisons efficiently.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Array plus Enumeration

bolt

Answer-first summary

Identify all peaks in a mountain array using direct array enumeration and strict neighbor comparisons efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus Enumeration

Try AiBox Copilotarrow_forward

To solve Find the Peaks, iterate through the array from the second to the second-to-last element, comparing each element with its immediate neighbors. Any element strictly greater than both neighbors is a peak and its index is recorded. This approach leverages array enumeration for simplicity and avoids unnecessary checks at boundaries, ensuring clear and efficient identification of peaks.

Problem Statement

Given a 0-indexed integer array mountain, your task is to determine which elements qualify as peaks. An element is a peak if it is strictly greater than its immediate left and right neighbors, excluding the first and last elements from consideration.

Return an array containing the indices of all peaks in any order. Ensure each peak satisfies the strict greater-than-neighbors condition and remember to skip boundary elements where peaks cannot occur.

Examples

Example 1

Input: mountain = [2,4,4]

Output: []

mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array. mountain[1] also can not be a peak because it is not strictly greater than mountain[2]. So the answer is [].

Example 2

Input: mountain = [1,4,3,8,5]

Output: [1,3]

mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array. mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1]. But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3].

Constraints

  • 3 <= mountain.length <= 100
  • 1 <= mountain[i] <= 100

Solution Approach

Linear Enumeration

Iterate from index 1 to length-2, checking if each element is strictly greater than its previous and next elements. Collect indices that meet this condition.

Boundary Awareness

Skip the first and last elements since they cannot be peaks. Focus only on the internal array slice where neighbors exist to prevent index errors.

Direct Comparison Efficiency

Use a single pass through the array with neighbor comparisons to maintain O(n) time and O(1) extra space, efficiently identifying all peaks without auxiliary structures.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) because each internal element is visited once. Space complexity is O(k) where k is the number of peaks, for storing their indices. No extra arrays are needed for computation.

What Interviewers Usually Probe

  • The candidate must correctly skip boundary elements.
  • Check if candidate uses strict greater-than comparison, not greater-than-or-equal.
  • Efficient single-pass iteration signals understanding of array enumeration pattern.

Common Pitfalls or Variants

Common pitfalls

  • Including first or last element as a peak incorrectly.
  • Using >= instead of >, leading to wrong peaks in flat regions.
  • Neglecting neighbor comparisons and returning indices blindly.

Follow-up variants

  • Return peaks in ascending or original index order instead of any order.
  • Find local minima instead of peaks using similar neighbor comparison logic.
  • Handle arrays with repeated elements by ignoring flat peaks between equal neighbors.

FAQ

What defines a peak in Find the Peaks problem?

A peak is an element strictly greater than both its immediate neighbors, excluding the first and last elements.

Can the first or last element ever be a peak?

No, the problem explicitly states peaks cannot include the first or last element of the array.

What is the optimal approach for Find the Peaks?

Use a single linear pass with direct neighbor comparisons from index 1 to length-2, collecting indices that satisfy the peak condition.

How does Array plus Enumeration pattern apply here?

By iterating the array with enumeration and comparing neighbors, the pattern ensures each element is evaluated systematically for peak conditions.

How do repeated values affect peak detection?

Peaks require strict greater-than comparisons, so flat regions with repeated values do not count as peaks.

terminal

Solution

Solution 1: Direct Traversal

We directly traverse the index $i \in [1, n-2]$. For each index $i$, if $mountain[i-1] < mountain[i]$ and $mountain[i + 1] < mountain[i]$, then $mountain[i]$ is a peak, and we add index $i$ to the answer array.

1
2
3
4
5
6
7
class Solution:
    def findPeaks(self, mountain: List[int]) -> List[int]:
        return [
            i
            for i in range(1, len(mountain) - 1)
            if mountain[i - 1] < mountain[i] > mountain[i + 1]
        ]
Find the Peaks Solution: Array plus Enumeration | LeetCode #2951 Easy