2951. Find the Peaks LeetCode Solution
In this guide, you will get 2951. Find the Peaks LeetCode Solution with the best time and space complexity. The solution to Find the Peaks problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Peaks solution in C++
- Find the Peaks solution in Java
- Find the Peaks solution in Python
- Additional Resources

Problem Statement of Find the Peaks
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.
Example 1:
Input: mountain = [2,4,4]
Output: []
Explanation: 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]
Explanation: 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
Complexity Analysis
- Time Complexity:
- Space Complexity:
2951. Find the Peaks LeetCode Solution in C++
class Solution {
public:
vector<int> findPeaks(vector<int>& mountain) {
vector<int> ans;
for (int i = 1; i + 1 < mountain.size(); ++i)
if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1])
ans.push_back(i);
return ans;
}
};
/* code provided by PROGIEZ */
2951. Find the Peaks LeetCode Solution in Java
class Solution {
public List<Integer> findPeaks(int[] mountain) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i + 1 < mountain.length; ++i)
if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1])
ans.add(i);
return ans;
}
}
// code provided by PROGIEZ
2951. Find the Peaks LeetCode Solution in Python
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]]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.