#C14156. Find Peak Elements

    ID: 43774 Type: Default 1000ms 256MiB

Find Peak Elements

Find Peak Elements

Given a list of integers, a peak element is defined as an element that is strictly greater than its neighbors. For an index \( i \) in a list of \( n \) elements:

  • If \(0 < i \texttt{nums}[i-1]\) and \(\texttt{nums}[i] > \texttt{nums}[i+1]\).
  • If \(i = 0\) (with \(n > 1\)), then the condition is \(\texttt{nums}[0] > \texttt{nums}[1]\).
  • If \(i = n-1\) (with \(n > 1\)), then the condition is \(\texttt{nums}[n-1] > \texttt{nums}[n-2]\).
  • If \(n = 1\), the single element is considered a peak by definition.

Your task is to read a list of integers from the input and output all of its peak elements in their original order.

inputFormat

The input begins with an integer (N) ((1 \leq N \leq 10^5)), representing the number of elements in the list. The second line contains (N) space-separated integers.

outputFormat

Output the peak elements in a single line, separated by a space. A newline character should follow the output.## sample

5
1 3 2 4 1
3 4

</p>