MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Wriggle_Sort_2/solution.cpp:1–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
3 void wiggleSort(vector<int>& nums) {
4
5 vector<int> nums_cpy=nums; //making a copy of the original array
6 sort(nums_cpy.begin(),nums_cpy.end()); //sorting the array
7 int i=1;
8 int j=nums.size()-1;
9 int k=0;
10
11 //for the 1st iteration, we copy numbers from the end of the sorted array
12 //to the odd positions of the original array
13 //in this way, the odd indices will be greater than its neighbours
14 while(i<nums.size()){
15 nums[i]=nums_cpy[j--];
16 i=i+2;
17
18 }
19 //resetting the i to store the even indices
20 i=0;
21
22 //for the 2nd iteration, we assign the even indices of the original array
23 //using the decreasing values from the sorted array
24 //in this iteration, even indices assigned will be less that its neighbouring
25 while(i<nums.size()){
26 nums[i]=nums_cpy[j--];
27 i=i+2;
28
29 }
30 }
31};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected