Snippets Collections
class Solution{

    //Function to swap two elements in the array.    
    public static void swap(int arr[], int x, int y){
        //Use of a temporary variable to swap the elements.
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }
    //Function to sort the array into a wave-like array.
    public static void convertToWave(int arr[], int n){
        
        //Iterating over the array. 
        for(int i=0;i<n-1;i=i+2){
            //Swapping two neighbouring elements at a time.
		    swap(arr, i, i+1);
		}
        return;
    }
    
}
star

Tue Feb 08 2022 05:46:24 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #wavearray

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension