Wave Array | Practice | GeeksforGeeks

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].....
If there are multiple solutions, find the lexicographically smallest one.
Example 1:
Input:
n = 5
arr[] = {1,2,3,4,5}
Output: 2 1 4 3 5
Explanation: Array elements after 
sorting it in wave form are 
2 1 4 3 5.
Example 2:
Input:
n = 6
arr[] = {2,4,7,8,9,10}
Output: 4 2 8 7 10 9
Explanation: Array elements after 
sorting it in wave form are 
4 2 8 7 10 9.
Your Task:
The task is to complete the function convertToWave(), which converts the given array to a wave array.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 106
0 ≤ arr[i] ≤107
0
rahulvishwas26024 days ago
 // Your code here       int j=1;       for(int i=0;i<n;i+=2){           if(i==n-1 && n%2==1)           {               break;           }          int temp=arr[i];          arr[i]=arr[i+1];          arr[i+1]=temp;       }
0
heynileshkapoor6 days ago
C++ | EASY | 0.1/1.41 
 
 
    void convertToWave(vector<int>& arr, int n){
        
        // Your code here
        int k;
        if(arr.size()%2==0)
        {
             k = arr.size();
        }
        else
        {
            k = arr.size()-1;
        }
        for(int i=0;i<k;i+=2)
        {
            arr[i] = arr[i]^arr[i+1];
            arr[i+1] = arr[i]^arr[i+1];//Using XOR to swap elements
            arr[i] = arr[i]^arr[i+1];
            
        }
        
    }

0
shekharankur46 days ago

class Solution{
    public:
    void convertToWave(vector<int>& arr, int n){
        
        // Your code here
        for(int i=0;i<n-1;i+=2){
            if(arr[i] < arr[i+1]){
                int x = arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=x;
            }
        }
    }
};


+1
user_qmho6 days ago
// Your code here       for(int i=0;i<n-1;i=i+2)       {           swap(arr[i],arr[i+1]);       }
0
chhetriarjun555551 week ago
C++
void convertToWave(vector<int>& arr, int n){       int i=0, j=0;      while(i < n/2){          swap(arr[j], arr[j+1]);          i++;          j += 2;      }   }
0
kushwaharajshree231 week ago
C++ solution just same approach as REVERSE ARRAY IN GROUPS...
 
 void convertToWave(vector<int>& arr, int n){
        
        // Your code here
        int k= 2;
        for(int i=0; i<n; i+=k)
        {
            int l=i;
            int r=min(i+k-1, n-1);
            while(l<=r)
            {
                int temp= arr[l];
                arr[l]=arr[r];
                arr[r]= temp;
                l++;
                r--;
            }
        }
        
    }
0
riyaraip081 week ago
// Your code here       int i=0,j=1;       for(i=0;i<n;i+=2)       {           if(j>=n){               break;           }           if(arr[i]<arr[j]){               swap(arr[i], arr[j]);           }           j += 2;       }   }
0
codewithshoaib191 week ago
public static void convertToWave(int arr[], int n){       // Your code here       for(int i=0;i<n-1;i+=2){           swap(arr,i,i+1);       }   }   public static void swap(int[]arr,int s,int e){       int temp=arr[s];       arr[s]=arr[e];       arr[e]=temp;   }
0
jayrambagal002 weeks ago
Python Solution:
 
def convertToWave(self,arr,N):       for i in range(0,N-1,2):           arr[i],arr[i+1]=arr[i+1],arr[i]       return arr
0
mayank180919992 weeks ago
 void convertToWave(vector<int>& arr, int n){
        
        // Your code here
        for(int i=0;i<n-1;i+=2){
            if(arr[i]<arr[i+1]){
                swap(arr[i],arr[i+1]);
            }
        }
        
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.