Wave Array | Practice | GeeksforGeeks

Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array(In Place).
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.
NOTE: The given array is sorted in ascending order, and you don't need to return anything to make changes in the original array itself.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 106
0 ≤ arr[i] ≤107
+1
omchoudhari17in 10 hours
C++ | 0.1s | EASY SOLUTION
 
class Solution {

    public:
    void convertToWave(int n, vector<int>& arr)
    {
        for(int i=0 ; i<n-1 ; i+=2)
            swap(arr[i], arr[i+1]);
    }
};
0
skpaul775in 9 hours
    def convertToWave(self, n : int, a : List[int]) -> None:       i=0       while i<n-1:           a[i], a[i+1] = a[i+1],a[i]           i+=2       return a
0
abhinayabhi141in 6 hours
class Solution {   public static void convertToWave(int n, int[] a) {       // code here        for (int i = 0; i < n; i += 2)           if (i != n - 1) swap(a, i, i + 1);   }
   private static void swap(int[] a, int i, int j) {       int temp = a[i];       a[i] = a[j];       a[j] = temp;   }}
0
tirthanprog3qnin 2 hours
Python Solution:
for i in range(1,n,2):           a[i],a[i-1]=a[i-1],a[i]       return a
0
sprakhar32112 hours ago
#Python slution
class Solution:   #Complete this function   #Function to sort the array into a wave-like array.   def convertToWave(self,arr,N):       #Your code here              for i in range(1,N,2):           arr[i], arr[i-1] = arr[i-1], arr[i]                  return arr
0
rsk09712 hours ago
C++ easy Solution:
void converToWave(int n vetor<int>& arr){
	for(int i = 0; i < n-1;i++){
	//we are swap the value 
	//array already sorted in order
	int temp = arr[i];
	arr[i]  = arr[i+1];
	arr[i+1] = temp;
	i++;
0
manzoorhussain442814 hours ago
Python Solution:
def convertToWave(self, n : int, a : List[int]) -> None:
        # code here
        arr = a
        for i in range(0,n-1,2):
            arr[i],arr[i+1] = arr[i+1],arr[i]
        return arr
0
manzoorhussain442814 hours ago
   int temp = 0;
        for (int i = 0;i<a.length-1;i+=2){
            // System.out.println(a[i]);
            temp = a[i];
            a[i] = a[i+1];
            a[i+1] = temp;
            
        }
0
ruchikajamwal03191 day ago
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){              // Your code here        for(int i = 0;i<n-1;i++){          if(i%2==0){              swap(arr[i+1],arr[i]);          }          else{              continue;          }        }   }};
+1
shashankpal19093 days ago
Easy Java Solution
class Solution {
    
    public static void convertToWave(int n, int[] a) {
        for (int i = 0; i < n; i += 2)
            if (i != n - 1) swap(a, i, i + 1);
    }

    private static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    
}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.