# Leetcode Wiggle Sort II problem solution

In this Leetcode Wiggle Sort II problem solution You are 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.

## Problem solution in Python.

```class Solution:
def wiggleSort(self, nums: List[int]) -> None:
nums.sort()
for i in range(len(nums)//2):
nums.insert(len(nums)//2 - i - 1,nums.pop())
if len(nums) % 2 != 0:
nums.insert(0,nums.pop())
nums.reverse()
if len(nums)>1 and nums[-1] == nums[-2]:
for i in range(len(nums)):
if nums[i] < nums[-2] and nums[-1] < nums[i+1]:
nums[i],nums[-1] = nums[-1],nums[i]
break
```

## Problem solution in Java.

```class Solution {
public void wiggleSort(int[] nums) {
int[] tempArr=Arrays.copyOf(nums,nums.length);
Arrays.sort(tempArr);
int t=tempArr.length-1;
for(int i=0;i<nums.length;i++){
if(i%2!=0){
nums[i]=tempArr[t];
tempArr[t]=-1;
--t;
}
}
t=0;
int k=0;
for(int i=nums.length-1;i>=0;i--){
if(i%2==0){
nums[i]=tempArr[t];
t++;
}
}
}
}
```

## Problem solution in C++.

```class Solution {
public:
void wiggleSort(vector<int>& nums) {
vector<int> sorted(nums);
sort(sorted.begin(),sorted.end());
int idx = sorted.size()-1;

for(int i=1; i<nums.size(); i+=2) nums[i] = sorted[idx--];

for(int i=0; i<nums.size(); i+=2) nums[i] = sorted[idx--];

return;
}
};
```