-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWave_Array.cpp
More file actions
41 lines (35 loc) · 958 Bytes
/
Wave_Array.cpp
File metadata and controls
41 lines (35 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// #include <algorithm>
// } Driver Code Ends
class Solution{
public:
// arr: input array
// n: size of array
//Function to sort the array into a wave-like array.
void convertToWave(int n, vector<int>& arr){
for(int i = 0; i < (n % 2 == 1 ? n - 1 : n); i++)
swap(arr[i], arr[i++]);
}
};
//{ Driver Code Starts.
int main()
{
int t,n;
cin>>t; //Input testcases
while(t--) //While testcases exist
{
cin>>n; //input size of array
vector<int> a(n); //declare vector of size n
for(int i=0;i<n;i++)
cin>>a[i]; //input elements of array
sort(a.begin(),a.end());
Solution ob;
ob.convertToWave(n, a);
for(int i=0;i<n;i++)
cout<<a[i]<<" "; //print array
cout<<endl;
}
}
// } Driver Code Ends