values. Rearrange its elements so that they alternate low and high:
values[0] ≤ values[1] ≥ values[2] ≤ values[3] ≥ values[4] ≤ …
Such an arrangement always exists, and usually there are several; any of them is accepted. Return the rearranged array.
Sorting solves it in O(n log n). Can you do it in a single pass?values=[4, 2, 9, 7, 1] → [2, 9, 4, 7, 1]
For example [2, 9, 4, 7, 1]: 2 ≤ 9 ≥ 4 ≤ 7 ≥ 1. Any other arrangement with the same alternation is fine too, say [1, 7, 2, 9, 4].
values=[5, 5, 5] → [5, 5, 5]
The inequalities are not strict, so equal numbers already satisfy them and the array can stay as it is.
values=[3, 8] → [3, 8]
values=[4, 2, 9, 7, 1]
[2, 9, 4, 7, 1]
Run checks the first 3 tests in your browser. Submit checks all 29 tests on the server, hidden ones included.