#K73557. Peak Elements in a Sequence

    ID: 34001 Type: Default 1000ms 256MiB

Peak Elements in a Sequence

Peak Elements in a Sequence

You are given a sequence of integers. Your task is to identify all peak elements in the sequence. An element in the sequence is called a peak element if it is strictly greater than both of its immediate neighbors. Formally, for an element \(a_i\) (with \(1 < i < n\)), it is a peak element if:

\(a_i > a_{i-1}\) and \(a_i > a_{i+1}\).

Note that the first and the last elements of the sequence are not considered since they have only one neighbor.

For example, given the sequence [2, 3, 4, 1, 5, 6, 4, 7], the peak elements are 4 and 6 because:

  • 4 is greater than 3 and 1.
  • 6 is greater than 5 and 4.

If no peak elements exist, output an empty line.

inputFormat

The input consists of two lines:

  1. The first line contains an integer \(n\) denoting the number of elements in the sequence.
  2. The second line contains \(n\) space-separated integers representing the sequence.

Input is read from standard input (stdin).

outputFormat

Output the peak elements found in the sequence, separated by a single space, on a single line. If there are no peak elements, output an empty line.

Output should be written to standard output (stdout).

## sample
8
2 3 4 1 5 6 4 7
4 6