#C7747. Zigzag Sequence Arrangement
Zigzag Sequence Arrangement
Zigzag Sequence Arrangement
You are given a list of power levels for spell books. Your task is to rearrange the given power levels into a zigzag sequence. First, sort the list in increasing order. Then, starting from the second element (index 1) up to the second-to-last element, swap every adjacent pair of numbers. Formally, for a sorted array (a_0, a_1, \dots, a_{n-1}), for every odd index (i) such that (1 \leq i \leq n-2) (with step 2), swap (a_i) and (a_{i+1}).
Constraints:
- \(1 \leq n \leq 100\)
- \(1 \leq a_i \leq 1000\)
Example: For (n = 6) and power levels ( [1, 3, 2, 4, 7, 6] ):
Sorted: [1, 2, 3, 4, 6, 7] Swap index 1 and 2: [1, 3, 2, 4, 6, 7] Swap index 3 and 4: [1, 3, 2, 6, 4, 7]
The final zigzag sequence is: 1 3 2 6 4 7
.
inputFormat
The input is given via standard input (stdin) with the following format:
The first line contains a single integer (n) that represents the number of spell books. The second line contains (n) space-separated integers representing the power levels of each book.
outputFormat
Output the power levels arranged into a zigzag sequence on a single line with each number separated by a space. The output should be printed to standard output (stdout).## sample
6
1 3 2 4 7 6
1 3 2 6 4 7