#K11096. Valley Permutation
Valley Permutation
Valley Permutation
You are given an integer n
and a list of n
distinct integers. Your task is to determine whether a valley permutation exists. A valley permutation is defined as a permutation where every element (except the first and last) is either a local minimum or a local maximum. In other words, for every index i (with 1 < i < n), the element at i should either be strictly less than both of its neighbors or strictly greater than both.
If n < 3
, it is impossible to form a valid valley permutation, and you should output NO
.
Otherwise, find any valley permutation of the array. The output should first print YES
on one line, followed by the permutation on the next line as space-separated integers.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains a single integer
n
— the number of elements. - The second line contains
n
space separated integers.
outputFormat
If a valley permutation is not possible (i.e. when n < 3
), output NO
.
Otherwise, output two lines: the first line should be YES
and the second line should contain the valley permutation as n
space-separated integers.
Your answer must be printed to standard output.
## sample5
1 2 3 4 5
YES
1 4 2 5 3
</p>