Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
You may assume the input array always has a valid answer.
Example 1:
Input: nums = [1,5,1,1,6,4] Output: [1,6,1,5,1,4] Explanation: [1,4,1,5,1,6] is also accepted.
Example 2:
Input: nums = [1,3,2,2,3,1] Output: [2,3,1,3,1,2]
Constraints:
1 <= nums.length <= 5 * 1040 <= nums[i] <= 5000nums.Follow Up: Can you do it in
O(n) time and/or in-place with O(1) extra space?Problem Overview: Wiggle Sort II asks you to reorder an array so that nums[0] < nums[1] > nums[2] < nums[3].... The tricky part is handling duplicates while keeping the strict wiggle pattern. A naive rearrangement often breaks when many values are equal, so the solution needs careful placement of small and large elements.
Approach 1: Sort and Merge (O(n log n) time, O(n) space)
The simplest strategy is to sort the array and then interleave the two halves. After sorting, split the array around the median. Place the smaller half in reverse order at even indices and the larger half in reverse order at odd indices. Reversing both halves ensures duplicates don't sit next to each other and break the wiggle condition. This approach relies on sorting and simple array iteration, making it easy to implement in interviews when optimal performance is not required.
Steps: sort the array, compute the midpoint, then iterate through the result array filling even indices from the end of the first half and odd indices from the end of the second half. Time complexity is O(n log n) due to sorting, and extra space O(n) if using a temporary array.
Approach 2: Tri-Partition with Virtual Indexing (O(n) time, O(1) space)
The optimal solution uses a combination of Quickselect and a three-way partition similar to the Dutch National Flag algorithm. First, find the median of the array in O(n) average time using Quickselect. The median becomes the pivot that separates smaller and larger values.
Instead of rearranging elements directly, apply virtual indexing. Map each logical position i to (1 + 2*i) % (n | 1). This mapping places larger elements in odd indices and smaller elements in even indices automatically. Then perform a three-way partition: elements greater than the median move to the front of the virtual order, elements smaller move to the back, and median values stay in the middle.
This technique ensures the final arrangement satisfies nums[0] < nums[1] > nums[2] < nums[3] even when many elements equal the median. The algorithm runs in O(n) time with O(1) extra space and heavily uses concepts from array manipulation and partition-based algorithms.
Recommended for interviews: Start with the sorting-based solution because it demonstrates clear reasoning and is easy to verify. Interviewers usually push for the optimal solution afterward. The Quickselect + virtual indexing method shows deeper understanding of partitioning, index mapping, and in-place algorithms, which is the solution expected at top companies.
This approach involves sorting the array and then dividing it into two halves. Afterward, wiggle sort by interleaving elements from each half.
Time Complexity: O(n log n)
Space Complexity: O(n)
The wiggleSort function sorts the array and splits it evenly into two parts. The first half fills the odd positions in reverse order, and the second half fills the even positions also in reverse.
Python
JavaScript
Java
Time Complexity: O(n log n), due to sorting.
Space Complexity: O(n) for splitting and temporary arrays.
This approach involves using the three-way partitioning technique around the median and arranges elements in a specific virtual order to ensure the wiggle property. This can be done in linear time and in place.
Time Complexity: O(n)
Space Complexity: O(1)
The solution defines functions to utilize quickselect to determine the median. It then rearranges the array using a virtual index mapped with a custom modulo logic to ensure in-place rearrangement with constant space.
Time Complexity: O(n) due to median finding and linear partitioning.
Space Complexity: O(1), as the solution rearranges in place.
Python
Java
C++
Go
JavaScript
| Approach | Complexity |
|---|---|
| Approach 1: Sort and Merge | Time Complexity: O(n log n), due to sorting. |
| Approach 2: Tri-Partition with Virtual Indexing | Time Complexity: O(n) due to median finding and linear partitioning. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sort and Merge | O(n log n) | O(n) | Best for clarity and quick implementation during interviews |
| Tri-Partition with Virtual Indexing (Quickselect) | O(n) | O(1) | Optimal solution when strict wiggle ordering and in-place rearrangement are required |
Wiggle Sort 2 | Leetcode 324 Solution in Hindi • Pepcoding • 18,571 views views
Watch 9 more video solutions →Practice Wiggle Sort II with our built-in code editor and test cases.
Practice on FleetCode