#K12776. Reordering Sequence to Minimize Adjacent Difference

    ID: 23766 Type: Default 1000ms 256MiB

Reordering Sequence to Minimize Adjacent Difference

Reordering Sequence to Minimize Adjacent Difference

Given a sequence of positive integers, your task is to reorder the sequence such that the maximum difference between any two adjacent elements is minimized. The intended strategy is to first sort the sequence and then create a new sequence by alternately selecting the smallest and the largest remaining numbers. This approach helps in bringing the extreme values closer, reducing the overall maximum difference.

For example, if the input sequence is [4, 2, 1, 7, 5], after sorting we get [1, 2, 4, 5, 7] and by alternately picking elements from the beginning and end, we obtain the final sequence: 1 7 2 5 4.

inputFormat

The input is given via standard input (stdin). The first line contains an integer n, representing the number of elements in the sequence. The second line contains n space-separated positive integers.

outputFormat

Print the reordered sequence as a single line of space-separated integers to standard output (stdout).## sample

5
4 2 1 7 5
1 7 2 5 4