AfterAcademy Tech
•
20 Aug 2020

Difficulty: Easy
Asked in: Amazon, Google, Adobe
You are given an unsorted array of integers(arr) of length n, write a program to sort it in wave form.
Problem Note:
arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] .....Example 1
Input: arr = [5, 2, 9, 3, 2]
Output: [2, 2, 5, 3, 9]
Explanation: In the above example, you can see 2 >= 2 <= 5 >= 3 <= 9. Thus we get, arr = [2, 2, 5, 3, 9] as output which is sorted in wave form.
Example 2
Input: arr = [3, 2, 9, 6, 4, 1]
Output: [2, 1, 4, 3, 9, 6]
Explanation: In the above example, you can see 2 >= 1 <= 4 >= 3 <= 9 >= 6. Thus we get, arr = [2, 1, 4, 3, 9, 6] as output which is sorted in wave form.
Example 3
Input: arr = [4, 2, 9, 1, 21, 43, 24]
Output: [2, 1, 9, 4, 24, 21, 43]
Explanation: In the above example, you can see 2 >= 1 <= 9 >= 4 <= 24 >= 21 <= 43. Thus we get, arr = [2, 1, 9, 4, 24, 21, 43] as output which is sorted in wave form.
For this problem, we will be discussing two possible solutions:-
You can try the problem here.
The problem asks that the array elements in the resultant array must be such that arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] ..... . So, one can find many different solutions for one input. However, the problem statement also requires to return the lexicographically smallest array. So, We can generalize the outputs in a simple way.
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
int[] waveArray(int arr[], int n) {
// Sort the input array
arr.sort()
// Swap adjacent elements
i = 0
while(i < n-1){
swap(arr[i], arr[i+1])
i = i + 2
}
return arr
}
Complexity Analysis
Time Complexity: O(n log n) (How?)
Space Complexity: O(1)
Critical Ideas To Think
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 — >
Pseudo Code
int[] waveArray(int arr[], int n) {
i = 0
while (i < n) {
// If current even index element is smaller than previous
if (i > 0 and arr[i-1] > arr[i] )
swap(arr[i], arr[i-1])
// If current even index element is smaller than next
if (i < n-1 and arr[i] < arr[i+1] )
swap(arr[i], arr[i + 1])
i = i + 2
}
return arr
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas To Think

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)

AfterAcademy Tech
The idea of this blog is to discuss the famous data structure called Array. An Array is one of the important data structures that are asked in the interviews. So, we will learn about arrays and we will also discuss about the idea of the dynamic array and its amortized analysis. So let's get started.

AfterAcademy Tech
In which situation 2 dimensional DP can be dropped to 1 dimension? Is there any principle or regular pattern? This is a very important question when it comes to optimization of DP arrays. Let's find out.

AfterAcademy Tech
Given an array of integers A[ ], if i < j and A[i] > A[j] then the pair (i, j) is called the inversion of an array A[ ]. You need to write a program to find the total counts of inversion in an array A[ ]. The inversion count of an array suggests how close the array is from being sorted. This problem has been asked in Amazon and Google's interview.
