Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
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 <= 1001 <= mountain[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,4,4]
[1,4,3,8,5]
find-peak-element)find-a-peak-element-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2951: Find the Peaks
class Solution {
public List<Integer> findPeaks(int[] mountain) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.add(i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2951: Find the Peaks
func findPeaks(mountain []int) (ans []int) {
for i := 1; i < len(mountain)-1; i++ {
if mountain[i-1] < mountain[i] && mountain[i+1] < mountain[i] {
ans = append(ans, i)
}
}
return
}
# Accepted solution for LeetCode #2951: Find the Peaks
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]
]
// Accepted solution for LeetCode #2951: Find the Peaks
fn find_peaks(mountain: Vec<i32>) -> Vec<i32> {
let mut ret = vec![];
for i in 1..(mountain.len() - 1) {
if mountain[i - 1] < mountain[i] && mountain[i] > mountain[i + 1] {
ret.push(i as i32);
}
}
ret
}
fn main() {
let mountain = vec![1, 4, 3, 8, 5];
let ret = find_peaks(mountain);
println!("ret={ret:?}");
}
#[test]
fn test() {
{
let mountain = vec![2, 4, 4];
let expected = vec![];
let ret = find_peaks(mountain);
assert_eq!(ret, expected);
}
{
let mountain = vec![1, 4, 3, 8, 5];
let expected = vec![1, 3];
let ret = find_peaks(mountain);
assert_eq!(ret, expected);
}
}
// Accepted solution for LeetCode #2951: Find the Peaks
function findPeaks(mountain: number[]): number[] {
const ans: number[] = [];
for (let i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push(i);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.