#K86152. Zigzag Sequence Formation
Zigzag Sequence Formation
Zigzag Sequence Formation
You are given an integer n and a sequence of n integers. Your task is to reorder the sequence (if possible) into a zigzag sequence such that:
- For every even index i (0-indexed), the element at position i is strictly greater than the element at position i+1.
- For every odd index i, the element at position i is strictly less than the element at position i+1.
If n = 1, the sequence is trivially a zigzag sequence.
If such an arrangement exists, print YES
on the first line and a valid zigzag sequence on the second line (all numbers separated by a space). Otherwise, print NO
on a single line.
Note: If there are multiple possible answers, outputting any one of them is acceptable.
Example:
Input: 5 4 1 3 2 5</p>Output: YES 5 1 4 2 3
inputFormat
The input is given via standard input (stdin) and has the following format:
- First line: an integer n, representing the number of elements in the sequence.
- Second line: n space-separated integers.
outputFormat
If a zigzag sequence can be formed, output should be:
- First line:
YES
- Second line: a rearranged sequence of n integers separated by spaces, satisfying the zigzag conditions.
If it is impossible to form such a sequence, output a single line containing NO
.
1
5
YES
5
</p>