Skip to interview prep
Apexed

29. Sort an Array Into a Wave

EasySorting

Rearrange nums into a wave where a[0] ≥ a[1] ≤ a[2] ≥ a[3] ≤ …. Many waves are possible, so produce this specific one: sort the numbers ascending, then swap the elements at positions (0, 1), (2, 3), (4, 5), and so on. Return the new array.

Example 1

Input: nums = [3, 6, 5, 10, 7, 20]
Output: [5, 3, 7, 6, 20, 10]

Constraints

  • 0 ≤ nums.length ≤ 10⁵

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Sort an Array Into a Wave — coding interview practice — Apexed