Search for a peak in an array

JavaScript Medium arrays

Task «Search for a peak in an array»

Given an array of integers, find the index of an element that is a peak, i.e., strictly greater than its neighbors. If there are several such elements, return the index of the first one found. Edge elements are considered peaks if they are greater than their only neighbor. The array is not empty. If there are no peaks, return -1.

Input Format

First line: integer N (1 ≤ N ≤ 1000) — the number of elements. Second line: N integers separated by spaces.

Output Format

One integer — the index of the first peak (0-based indexing) or -1 if there are no peaks.

main.js