Back to DSA sheet

Wiggle Sort

MediumArrays

Reorder the array in place so it satisfies nums[0] <= nums[1] >= nums[2] <= nums[3] >= ...; any valid wiggle arrangement is accepted.

Input format (stdin): the first line has n. The second line has n integers. Output a valid wiggle ordering produced by the single-pass swap method.

Examples
Input: 6 3 5 2 1 6 4
Output: 3 5 1 6 2 4
Each pair alternates <= then >=.
Input: 3 1 2 3
Output: 1 3 2
Constraints
  • 1 <= n <= 10^4
  • 0 <= nums[i] <= 10^4
Sheets
NeetCode 250
wiggle-sort.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.