Super-rocky Terrain#11842

Super-rocky Terrain (Editorial)

One of the possible arrangements is

i.e. in the sorted permutation swap positions and for all k=1,\dots,\lfloor(n-1)/2\rfloor. Time complexity because we know all the elements since it's a permutation, so no sorting is needed.

The original harder version used an arbitrary array of numbers. To solve it, sort to obtain b_1,\dots,b_n; split into small=\{b_1,\dots,b_{\lceil n/2\rceil}\} and large=\{b_{\lceil n/2\rceil+1},\dots,b_n\}. Build res = \{small_{\text{last}},\,large_{\text{last}},\,small_{\text{last}-1},\dots\} and verify the zig-zag condition \,res_1<res_2>res_3<res_4\dots Overall complexity O(n\log n) due to the sort.