#C9299. Alternating Sort

    ID: 53376 Type: Default 1000ms 256MiB

Alternating Sort

Alternating Sort

You are given a list of n integers. Your task is to rearrange the list into an alternating order, where the first element is the smallest, the second element is the largest, the third element is the second smallest, the fourth is the second largest, and so on.

Formally, if the sorted list is \(a_1 \le a_2 \le \cdots \le a_n\), you must produce the list \([a_1, a_n, a_2, a_{n-1}, \ldots]\). If the list has an odd number of elements, the middle element will just appear once at the end of the arrangement.

This problem tests your ability to manipulate arrays and use two-pointer techniques.

inputFormat

The first line contains a single integer (n) ((1 \le n \le 10^5)), the number of elements in the list. The second line contains (n) space-separated integers.

outputFormat

Output a single line containing the alternately sorted list elements separated by a space.## sample

5
5 3 1 4 2
1 5 2 4 3

</p>