An array is in wave form if its elements follow the pattern arr[0] >= arr[1] <= arr[2] >= arr[3].... The task is to rearrange the elements so that every even-indexed element is greater than or equal to its adjacent elements.
- The array can be converted into wave form using sorting or a single-pass in-place approach.
- The single-pass approach takes O(n) time and O(1) auxiliary space.
Examples:
Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80}
Output: 10 5 6 2 20 3 100 80Input: arr[] = {3, 6, 5, 10, 7, 20}
Output: 6 3 10 5 20 7Multiple valid wave arrangements may exist for the same input array.
Approaches to Sort an Array in Wave Form
The array can be rearranged into wave form using the following approaches:
1. Sorting and Swapping Adjacent Elements
The simple approach first sorts the array in ascending order. After sorting, every pair of adjacent elements is swapped, which produces the required wave pattern.
Steps
- Sort the array in ascending order.
- Start from index 0.
- Swap the elements at indices i and i + 1.
- Move i forward by 2 and repeat.
- Stop when there is no complete pair left.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to convert the array into wave form
void sortInWave(vector<int>& arr)
{
// Sort the array in ascending order
sort(arr.begin(), arr.end());
// Swap adjacent elements
// at even indices
for (int i = 0; i + 1 < arr.size(); i += 2)
{
swap(arr[i], arr[i + 1]);
}
}
int main()
{
vector<int> arr = {10, 90, 49, 2, 1, 5, 23};
// Convert the array into wave form
sortInWave(arr);
// Print the wave array
for (int x : arr)
cout << x << " ";
return 0;
}
Output
2 1 10 5 49 23 90
Explanation: After sorting, the array becomes: 1 2 5 10 23 49 90
- Swapping adjacent elements starting from index 0 gives: 2 1 10 5 49 23 90
- This satisfies: 2 >= 1 <= 10 >= 5 <= 49 >= 23 <= 90
2. Single Traversal Using Local Comparisons
The wave condition can be achieved without sorting the entire array. For each even index, compare it with its neighbors and swap when needed to make it greater than or equal to them.
- If the previous element is greater, swap them.
- If the next element is greater, swap them.
Steps
- Traverse the even indices 0, 2, 4, ....
- Compare the current element with its previous element.
- If the previous element is greater, swap them.
- Compare the current element with its next element.
- If the next element is greater, swap them.
- Continue until all even positions are processed.
#include <iostream>
#include <vector>
using namespace std;
// Function to convert the array into wave form
void sortInWave(vector<int>& arr)
{
int n = arr.size();
// Process all even indices
for (int i = 0; i < n; i += 2)
{
// Make sure arr[i] >= arr[i - 1]
if (i > 0 && arr[i] < arr[i - 1])
{
swap(arr[i], arr[i - 1]);
}
// Make sure arr[i] >= arr[i + 1]
if (i + 1 < n && arr[i] < arr[i + 1])
{
swap(arr[i], arr[i + 1]);
}
}
}
int main()
{
vector<int> arr = {10, 90, 49, 2, 1, 5, 23};
// Convert the array into wave form
sortInWave(arr);
// Print the wave array
for (int x : arr)
cout << x << " ";
return 0;
}
Output
90 10 49 1 5 2 23
Explanation: The algorithm processes only the even indices. At each such index, it makes the current element greater than or equal to its neighbors. For example, when processing index 2, the algorithm checks the relationship with index 1 and index 3. After the necessary swaps, the condition
arr[1] <= arr[2] >= arr[3]
is satisfied. The process continues for the remaining even indices. Since each element is involved in only a constant number of comparisons and swaps, the entire array is converted into wave form in linear time.