Find the Peaks

easy array

Problem

You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array. 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 peaks. Return the indices of all peaks in any order.

Inputmountain = [1,4,3,8,5]
Output[1,3]
Index 1 (4 > 1 and 4 > 3) and index 3 (8 > 3 and 8 > 5) are peaks; the ends are excluded.

def find_peaks(mountain):
    peaks = []
    for i in range(1, len(mountain) - 1):
        if mountain[i] > mountain[i - 1] and mountain[i] > mountain[i + 1]:
            peaks.append(i)
    return peaks
function findPeaks(mountain) {
  const peaks = [];
  for (let i = 1; i < mountain.length - 1; i++) {
    if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1]) {
      peaks.push(i);
    }
  }
  return peaks;
}
class Solution {
    public List<Integer> findPeaks(int[] mountain) {
        List<Integer> peaks = new ArrayList<>();
        for (int i = 1; i < mountain.length - 1; i++) {
            if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1]) {
                peaks.add(i);
            }
        }
        return peaks;
    }
}
vector<int> findPeaks(vector<int>& mountain) {
    vector<int> peaks;
    for (int i = 1; i < (int)mountain.size() - 1; i++) {
        if (mountain[i] > mountain[i - 1] && mountain[i] > mountain[i + 1]) {
            peaks.push_back(i);
        }
    }
    return peaks;
}
Time: O(n) Space: O(1)