Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
468 B
2
Indexable
class Solution {
public:
    void wiggleSort(vector<int>& nums) { //fill odd inciddes with latger elements of num and the rest in even indices
        sort(nums.begin(),nums.end());
        int s=1,e=nums.size()-1;
        vector<int> tmp(nums.size());
        while(s<nums.size()){
            tmp[s]=nums[e--];
            s+=2;
        }
        s=0;
        while(s<nums.size()){
            tmp[s]=nums[e--];
            s+=2;
        }
        nums=tmp;
    }
};