Given an array arr[] of integers sorted in ascending order, rearrange the array into a wave-like array such that: arr[0] β₯ arr[1] β€ arr[2] β₯ arr[3] β€ arr[4] ...
If multiple wave-like arrangements are possible, find the lexicographically smallest arrangement.
Note: The array should be modified in-place.
Input: arr[] = [1, 2, 3, 4, 5]
Output: [2, 1, 4, 3, 5]
Explanation: Array elements after sorting it in the waveform are 2, 1, 4, 3, 5.Input: arr[] = [2, 4, 7, 8, 9, 10]
Output: [4, 2, 8, 7, 10, 9]
Explanation: Array elements after sorting it in the waveform are 4, 2, 8, 7, 10, 9.
Adjacent Pair Swapping Method - O(n) Time and O(1) O(1) Auxiliary Space
The main idea of this approach is to rearrange it into a wave form by swapping adjacent elements in pairs.
Since, the elements are in increasing order. Then, by swapping every pair of adjacent elements (i.e., arr[0] with arr[1], arr[2] with arr[3], and so on), we create a pattern where:
- Every even index i holds a value greater than or equal to its neighboring odd indices i - 1 and i + 1 (if they exist).
- This guarantees the wave condition: arr[0] β₯ arr[1] β€ arr[2] β₯ arr[3] β€ arr[4] β₯ ...
The sorted array ensures the numbers are close enough to form valid wave peaks and valleys, and the pairwise swaps are what enforce the pattern.
The resulting array is also lexicographically smallest.
- For the first pair a0 β€ a1 , the wave condition requires arr[0] β₯ arr[1].
- So a0 cannot be placed at arr[0]. The smallest possible value is a1 , giving [a1 , a0].
- The same logic applies to every adjacent pair.
- Thus, swapping every adjacent pair gives: [a1, a0, a3 ,a2 , a5 , a4, ...]
This arrangement satisfies the wave condition and is the lexicographically smallest valid wave array.
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
void sortInWave(vector<int> &arr){
int n = arr.size();
// swap adjacent elements to create
// wave pattern
for (int i = 0; i < n - 1; i += 2){
swap(arr[i], arr[i + 1]);
}
}
int main(){
vector<int> arr = {1, 2, 3, 4, 5};
sortInWave(arr);
for (int i=0; i<arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
#include<stdio.h>
#include<stdlib.h>
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
void sortInWave(int arr[], int n){
// swap adjacent elements
for (int i=0; i<n-1; i += 2)
swap(&arr[i], &arr[i+1]);
}
int main(){
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
sortInWave(arr, n);
for (int i=0; i<n; i++)
printf("%d ",arr[i]);
return 0;
}
class GfG{
static void sortInWave(int arr[]){
int n = arr.length;
// swap adjacent elements
for (int i=0; i<n-1; i += 2){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
public static void main(String args[]){
int arr[] = {1, 2, 3, 4, 5};
sortInWave(arr);
for (int i : arr){
System.out.print(i + " ");
}
}
}
def sortInWave(arr):
n = len(arr)
# swap adjacent elements
for i in range(0,n-1,2):
arr[i], arr[i+1] = arr[i+1], arr[i]
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
sortInWave(arr)
for i in range(0,len(arr)):
print (arr[i],end=" ")
using System;
class GFG
{
static void sortInWave(int[] arr)
{
int n = arr.Length;
// swap adjacent elements
for (int i = 0; i < n - 1; i += 2)
{
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
sortInWave(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
// utility function to swap two elements
function swap(arr, x, y) {
let temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
function sortInWave(arr) {
// swap adjacent elements
for (let i = 0; i < arr.length - 1; i += 2) {
swap(arr, i, i + 1);
}
}
// Driver code
let arr = [1, 2, 3, 4, 5];
sortInWave(arr);
console.log(arr.join(" "));
Output
2 1 4 3 5