forked from sakshamchecker/Hacktoberfest-21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wave_array.cpp
42 lines (34 loc) · 1.06 KB
/
Wave_array.cpp
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
42
//Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it
//In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....
#include<bits/stdc++.h>
using namespace std;
// } 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(vector<int>& arr, int n){
// Your code here
for(int i=1;i<n;i=i+2)
swap(arr[i],arr[i-1]);
}
};
// { 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
Solution ob;
ob.convertToWave(a, n);
for(int i=0;i<n;i++)
cout<<a[i]<<" "; //print array
cout<<endl;
}
} // } Driver Code Ends