0% found this document useful (0 votes)
11 views9 pages

Wave Array

Uploaded by

lonesome.rar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Wave Array

Uploaded by

lonesome.rar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1/2/25, 12:17 PM Wave Array

AfterAcademy

Admin AfterAcademy
20 Aug 2020

Wave Array

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:

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.

The array may contain duplicates.

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 w

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. T

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

Solutions
For this problem, we will be discussing two possible solutions:-

1. Sorting and swapping: Sort the array and swap the adjacent values.

2. Comparing neighbours: Compare neighbouring elements and swap if


the problem condition doesn’t satisfy.

You can try the problem here.


https://afteracademy.com/blog/wave-array/ 2/9
1/2/25, 12:17 PM Wave Array

1. Sorting and Swapping


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

Sort the array in ascending order.

Swap the adjacent elements.

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

https://afteracademy.com/blog/wave-array/ 3/9
1/2/25, 12:17 PM Wave Array

Time Complexity: O(n log n) (How?)

Space Complexity: O(1)

Critical Ideas To Think

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?

Is the solution is lexicographically smallest answer? Answer: Yes

Try to optimize the running time complexity.

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

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

Does the solution return the lexicographically smallest array? If No,


Why?

Do you think any solution is possible which guarantees


lexicographically smallest array with linear run-time complexity?

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

Comparison of Different Solutions

Suggested Problems To Solve


Check if an array is a 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 .

Sort array with squares

Sort array of 0s, 1s, and 2s.

If you have any more approaches or you find an error/bug in the above
solutions, please comment down below.

Happy Coding!

Enjoy Algorithms!

Recommended for You

https://afteracademy.com/blog/wave-array/ 6/9
1/2/25, 12:17 PM Wave Array

Average of Levels in Recursive Insertion Sort


Binary Tree Write a program for the recursive
Given a binary tree, write a program to implementation of Insertion Sort. Insertion
return the average value of the nodes on Sort is used to sort a given array. This
each level in the form of an array. The range problem will sharpen your recursion skills.
of the node's value is in the range of 32-bit
signed integer.

Admin AfterAcademy Admin AfterAcademy


5 Nov 2020 23 Sep 2020

When to Convert a 2-D DP Sort List - Merge Sort


array to 1-D DP array Sort a linked list using Merge Sort. This is a
And How? very famous interview problem that
demonstrates the concept of recursion. This
In which situation 2 dimensional DP can be
problem is quite similar to Merge Sort in
dropped to 1 dimension? Is there any
Arrays.
principle or regular pattern? This is a very
important question when it comes to
optimization of DP arrays. Let's find out.

Admin AfterAcademy Admin AfterAcademy


16 Sep 2020 12 Sep 2020

https://afteracademy.com/blog/wave-array/ 7/9
1/2/25, 12:17 PM Wave Array

Largest Element In An Array Merge Two BST


Given an array arr[] of size n , write a Given two binary search trees with root
program to find the largest element in it. To nodes as tree1 and tree2 of size n and m,
find the largest element we can just traverse write a program to return an array of
the array in one pass and find the largest integers that contains all the elements of
element by maintaining a max variable. tree1 and tree2 in non-decreasing order. The
expected time complexity is O(m+n).

Admin AfterAcademy Admin AfterAcademy


4 Sep 2020 1 Sep 2020

Connect With Your Mentors

https://afteracademy.com/blog/wave-array/ 8/9
1/2/25, 12:17 PM Wave Array

Janishar Ali Amit Shekhar


Founder | IIT-BHU | 10 Yrs Exp. Founder | IIT-BHU | 10 Yrs Exp.

Copyright 2022, MindOrks Nextgen Private Limited

https://afteracademy.com/blog/wave-array/ 9/9

You might also like