|
| 1 | +--- |
| 2 | +id: wiggle-sort-ii |
| 3 | +title: Wiggle Sort II |
| 4 | +sidebar_label: 0324 - Wiggle Sort II |
| 5 | +tags: |
| 6 | +- Array |
| 7 | +- Divide and Conquer |
| 8 | +- Greedy |
| 9 | +- Sorting |
| 10 | +- Quickselect |
| 11 | + |
| 12 | +description: "This is a solution to the Wiggle Sort II problem on LeetCode." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Description |
| 16 | +Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... |
| 17 | +You may assume the input array always has a valid answer. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: nums = [1,5,1,1,6,4] |
| 25 | +Output: [1,6,1,5,1,4] |
| 26 | +Explanation: [1,4,1,5,1,6] is also accepted. |
| 27 | +
|
| 28 | +``` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | +``` |
| 32 | +Input: nums = [1,3,2,2,3,1] |
| 33 | +Output: [2,3,1,3,1,2] |
| 34 | +``` |
| 35 | + |
| 36 | +### Constraints |
| 37 | + |
| 38 | +- `1 <= nums.length <= 5 * 10^4` |
| 39 | +- `0 <= nums[i] <= 5000` |
| 40 | +- It is guaranteed that there will be an answer for the given input nums. |
| 41 | + |
| 42 | +## Solution for Wiggle Sort II Problem |
| 43 | +### Approach |
| 44 | +#### Sorting the Array: |
| 45 | +- The first step is to sort the input array nums. This ensures that the elements are in non-decreasing order. |
| 46 | +- Sorting helps to easily pick the smallest and largest remaining elements in subsequent steps. |
| 47 | +#### Creating a Temporary Array: |
| 48 | +- A temporary array temp of the same size as nums is created to store the elements in a "wiggle" pattern. |
| 49 | +- An index variable j is initialized to point to the last element of the sorted nums array. This will help in filling the temporary array from the largest to the smallest element. |
| 50 | +#### Filling Odd Indices: |
| 51 | +- A loop runs from the index 1 to the end of the array with a step of 2 (i.e., 1, 3, 5, ...). This loop fills the odd indices of the temp array. |
| 52 | +- The largest remaining elements from nums are placed at these odd indices. The index j is decremented after placing each element. |
| 53 | +#### Filling Even Indices: |
| 54 | +- Another loop runs from the index 0 to the end of the array with a step of 2 (i.e., 0, 2, 4, ...). This loop fills the even indices of the temp array. |
| 55 | +- The largest remaining elements from nums are placed at these even indices. The index j is decremented after placing each element. |
| 56 | +- Copying Back to Original Array: |
| 57 | +- The temp array, now containing the elements in the desired "wiggle" order, is copied back to the original nums array. |
| 58 | + |
| 59 | + |
| 60 | +<Tabs> |
| 61 | + <TabItem value="Solution" label="Solution"> |
| 62 | + |
| 63 | + #### Implementation |
| 64 | + ```jsx live |
| 65 | + function Solution(arr) { |
| 66 | + function wiggleSort(nums) { |
| 67 | + nums.sort((a, b) => a - b); |
| 68 | + const temp = new Array(nums.length); |
| 69 | + let j = nums.length - 1; |
| 70 | + for (let i = 1; i < nums.length; i += 2) { |
| 71 | + temp[i] = nums[j--]; |
| 72 | + } |
| 73 | + for (let i = 0; i < nums.length; i += 2) { |
| 74 | + temp[i] = nums[j--]; |
| 75 | + } |
| 76 | + return temp; |
| 77 | + } |
| 78 | + const input = [1, 5, 1, 1, 6, 4]; |
| 79 | + const output = wiggleSort(input); |
| 80 | + return ( |
| 81 | + <div> |
| 82 | + <p> |
| 83 | + <b>Input: </b> |
| 84 | + {JSON.stringify(input)} |
| 85 | + </p> |
| 86 | + <p> |
| 87 | + <b>Output:</b> {output.toString()} |
| 88 | + </p> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + } |
| 92 | + ``` |
| 93 | + |
| 94 | + #### Complexity Analysis |
| 95 | + |
| 96 | + - Time Complexity: $ O(nlogn) $ is the time complexity, where n is the size of array |
| 97 | + - Space Complexity: $ O(n) $ , because of the Temp array we have taken. |
| 98 | + |
| 99 | + ## Code in Different Languages |
| 100 | + <Tabs> |
| 101 | + <TabItem value="JavaScript" label="JavaScript"> |
| 102 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 103 | + ```javascript |
| 104 | + function wiggleSort(nums) { |
| 105 | + nums.sort((a, b) => a - b); |
| 106 | + const temp = new Array(nums.length); |
| 107 | + let j = nums.length - 1; |
| 108 | + for (let i = 1; i < nums.length; i += 2) { |
| 109 | + temp[i] = nums[j--]; |
| 110 | + } |
| 111 | + for (let i = 0; i < nums.length; i += 2) { |
| 112 | + temp[i] = nums[j--]; |
| 113 | + } |
| 114 | + for (let i = 0; i < nums.length; i++) { |
| 115 | + nums[i] = temp[i]; |
| 116 | + } |
| 117 | + } |
| 118 | + ``` |
| 119 | + |
| 120 | + </TabItem> |
| 121 | + <TabItem value="TypeScript" label="TypeScript"> |
| 122 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 123 | + ```typescript |
| 124 | + function wiggleSort(nums: number[]): void { |
| 125 | + nums.sort((a, b) => a - b); |
| 126 | + const temp: number[] = new Array(nums.length); |
| 127 | + let j = nums.length - 1; |
| 128 | + |
| 129 | + for (let i = 1; i < nums.length; i += 2) { |
| 130 | + temp[i] = nums[j--]; |
| 131 | + } |
| 132 | + for (let i = 0; i < nums.length; i += 2) { |
| 133 | + temp[i] = nums[j--]; |
| 134 | + } |
| 135 | + |
| 136 | + for (let i = 0; i < nums.length; i++) { |
| 137 | + nums[i] = temp[i]; |
| 138 | + } |
| 139 | +} |
| 140 | +
|
| 141 | +// Example usage |
| 142 | + const input: number[] = [1, 5, 1, 1, 6, 4]; |
| 143 | + wiggleSort(input); |
| 144 | + console.log(input); // Output might be [1, 6, 1, 5, 1, 4] |
| 145 | + ``` |
| 146 | + |
| 147 | + </TabItem> |
| 148 | + <TabItem value="Python" label="Python"> |
| 149 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 150 | + ```python |
| 151 | + def wiggle_sort(nums): |
| 152 | + nums.sort() |
| 153 | + temp = [0] * len(nums) |
| 154 | + j = len(nums) - 1 |
| 155 | + |
| 156 | + for i in range(1, len(nums), 2): |
| 157 | + temp[i] = nums[j] |
| 158 | + j -= 1 |
| 159 | + for i in range(0, len(nums), 2): |
| 160 | + temp[i] = nums[j] |
| 161 | + j -= 1 |
| 162 | + |
| 163 | + for i in range(len(nums)): |
| 164 | + nums[i] = temp[i] |
| 165 | +
|
| 166 | +# Example usage |
| 167 | +input = [1, 5, 1, 1, 6, 4] |
| 168 | +wiggle_sort(input) |
| 169 | +print(input) # Output might be [1, 6, 1, 5, 1, 4] |
| 170 | +
|
| 171 | + ``` |
| 172 | + |
| 173 | + </TabItem> |
| 174 | + <TabItem value="Java" label="Java"> |
| 175 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 176 | + ```java |
| 177 | + import java.util.Arrays; |
| 178 | +
|
| 179 | +public class WiggleSort { |
| 180 | + public static void wiggleSort(int[] nums) { |
| 181 | + Arrays.sort(nums); |
| 182 | + int[] temp = new int[nums.length]; |
| 183 | + int j = nums.length - 1; |
| 184 | + |
| 185 | + for (int i = 1; i < nums.length; i += 2) { |
| 186 | + temp[i] = nums[j--]; |
| 187 | + } |
| 188 | + for (int i = 0; i < nums.length; i += 2) { |
| 189 | + temp[i] = nums[j--]; |
| 190 | + } |
| 191 | + |
| 192 | + System.arraycopy(temp, 0, nums, 0, nums.length); |
| 193 | + } |
| 194 | +
|
| 195 | + public static void main(String[] args) { |
| 196 | + int[] input = {1, 5, 1, 1, 6, 4}; |
| 197 | + wiggleSort(input); |
| 198 | + System.out.println(Arrays.toString(input)); // Output might be [1, 6, 1, 5, 1, 4] |
| 199 | + } |
| 200 | +} |
| 201 | +
|
| 202 | + ``` |
| 203 | + |
| 204 | + </TabItem> |
| 205 | + <TabItem value="C++" label="C++"> |
| 206 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 207 | + ```cpp |
| 208 | + class Solution { |
| 209 | +public: |
| 210 | + void wiggleSort(vector<int>& nums) { |
| 211 | + sort(nums.begin() , nums.end()); |
| 212 | + vector<int>temp(nums.size()); |
| 213 | + int j=nums.size()-1; |
| 214 | + for(int i=1;i<nums.size();i+=2) |
| 215 | + { |
| 216 | + temp[i]=nums[j--]; |
| 217 | + } |
| 218 | + for(int i=0;i<nums.size();i+=2) |
| 219 | + { |
| 220 | + temp[i]=nums[j--]; |
| 221 | + } |
| 222 | + nums=temp; |
| 223 | + } |
| 224 | +}; |
| 225 | + ``` |
| 226 | +</TabItem> |
| 227 | +</Tabs> |
| 228 | + |
| 229 | + </TabItem> |
| 230 | +</Tabs> |
| 231 | + |
| 232 | +## References |
| 233 | + |
| 234 | +- **LeetCode Problem**: [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/description/) |
| 235 | + |
| 236 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/wiggle-sort-ii/solution) |
| 237 | + |
0 commit comments