#K63517. Transform an Array into a Zebra Pattern
Transform an Array into a Zebra Pattern
Transform an Array into a Zebra Pattern
You are given an array of integers. Your task is to transform the array into a zebra patterned sequence such that for every odd index i
(0-indexed), the element at a[i]
is greater than or equal to its adjacent neighbors. In other words, for every odd index i
(with 0 ≤ i < n
), the following conditions must hold:
$$a[i] \ge a[i-1]$$ $$\text{and if } i+1<n,\; a[i] \ge a[i+1]$$
If the array has only one element, it should remain unchanged. The transformation is performed by swapping adjacent elements when necessary.
Example 1:
Input: 1 3 2 4 5 6
Output: 1 3 2 5 4 6
Example 2:
Input: 1 2 3 4
Output: 1 3 2 4
inputFormat
The input is given as a single line of space-separated integers, representing the elements of the array.
outputFormat
Output a single line of space-separated integers representing the transformed zebra-patterned array.
## sample7
7