Problem 1: Wave Array Gfg

Problem 1: Wave Array Gfg

the problem states that we have to convert a sorted array to a wave array means if the input is

input: 1 2 3 4 5

output: 2 1 4 3 5

so it is a straightforward solution we have to traverse and swap in pair of two numbers.

#include<bits/stdc++.h>
using namespace std;

class Solution{
    public:

    void convertToWave(int n, vector<int>& arr){

        // Your code here
        int i=0,j=1;
        while(j<n)
        {
            swap(arr[i],arr[j]);
            i=j+1;
            j=i+1;
        }

    }
};


int main()
{
    int t,n;
    cin>>t; 
    while(t--) 
    {
        cin>>n; 
        vector<int> a(n); 
        for(int i=0;i<n;i++)
            cin>>a[i]; 
        Solution ob;
        ob.convertToWave(n, a);

        for(int i=0;i<n;i++)
            cout<<a[i]<<" "; 

        cout<<endl;
    }
}