Wave Array
Wave Array
AfterAcademy
Admin AfterAcademy
20 Aug 2020
Wave Array
Difficulty: Easy
Problem Note:
https://afteracademy.com/blog/wave-array/ 1/9
1/2/25, 12:17 PM Wave Array
The array elements in the resultant array must be such that arr[0] >=
arr[1] <= arr[2] >= arr[3] <= arr[4] .....
If there are multiple sorted orders in wave form, return the one which
is lexicographically smallest.
Example 1
Example 2
Example 3
Solutions
For this problem, we will be discussing two possible solutions:-
1. Sorting and swapping: Sort the array and swap the adjacent values.
We can directly sort the input array so that arr[0] <= arr[1] <= arr[2]
<= arr[3] <= arr[4] ..... now, swapping the alternate elements i.e.
arr[0] with arr[1] and arr[2] with arr[3] and arr[4] with
arr[5] and …, we will have the resultant array satisfying the problem
condition.
Solutions steps
Pseudo Code
Complexity Analysis
https://afteracademy.com/blog/wave-array/ 3/9
1/2/25, 12:17 PM Wave Array
Can we solve this problem by sorting the array in decreasing order and
then swapping adjacent values?
The problem requires the array to satisfy just the relationship between
the neighbouring elements, so is sorting even required?
2. Comparing Neighbors
We can optimize the previous approach as we can only focus on
maintaining the condition for neighbouring elements. However, this will
not guarantee the lexicographically smallest array in return.
If we make sure that all even positioned (at index 0, 2, 4, ..) elements are
greater than their adjacent odd positioned elements, we won’t have to
check the oddly positioned element.
Solution Steps
Traverse all even positioned elements of the input array, and do the
following — >
If the current element is smaller than the previous odd element, swap
previous and current.
If the current element is smaller than the next odd element, swap next
and current.
https://afteracademy.com/blog/wave-array/ 4/9
1/2/25, 12:17 PM Wave Array
Pseudo Code
Complexity Analysis
How did we make sure that only comparing the neighbouring elements
and swapping elements as per the required condition will satisfy the
problem condition?
If the input array is already sorted, then the output array according to
this approach will be a lexicographically smallest array. True or False?
Looking at the pseudo-code, what are the previous and next elements
for each current element?
https://afteracademy.com/blog/wave-array/ 5/9
1/2/25, 12:17 PM Wave Array
Find if an integer p exists in the array such that the number of integers
greater than p in the array equals to p .
If you have any more approaches or you find an error/bug in the above
solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
https://afteracademy.com/blog/wave-array/ 6/9
1/2/25, 12:17 PM Wave Array
https://afteracademy.com/blog/wave-array/ 7/9
1/2/25, 12:17 PM Wave Array
https://afteracademy.com/blog/wave-array/ 8/9
1/2/25, 12:17 PM Wave Array
https://afteracademy.com/blog/wave-array/ 9/9