#K64462. Find Peaks in an Array
Find Peaks in an Array
Find Peaks in an Array
You are given an array of integers. Your task is to find and output the indices of all the peaks in the array. An element at index i is considered a peak if:
- For 0 < i < n-1: the element is strictly greater than both of its immediate neighbors, i.e. \(a_i > a_{i-1}\) and \(a_i > a_{i+1}\).
- For the first element (i = 0), if the array has more than one element, it is a peak if \(a_0 > a_1\).
- For the last element (i = n-1), if the array has more than one element, it is a peak if \(a_{n-1} > a_{n-2}\).
If there are no peaks, output an empty line.
Note: If the array is empty or contains only one element, there are no peaks.
inputFormat
The input is read from standard input. The first line contains a non-negative integer (n), representing the number of elements in the array. The second line contains (n) space-separated integers, representing the array elements. If (n = 0), the second line may be empty.
outputFormat
Output the indices of the peaks in a single line separated by a space. If there are no peaks, output an empty line.## sample
7
1 2 1 3 5 6 4
1 5