Wave Array Sorting Problem
Your goal is to sort a given unsorted array ARR
such that it forms a wave array.
Explanation:
After sorting, the array should satisfy the wave pattern conditions:
ARR[0] >= ARR[1]
ARR[1] <= ARR[2]
ARR[2] >= ARR[3]
ARR[3] <= ARR[4]
- And so on...
Multiple solutions may exist, but you need to return only one valid wave array.
Example:
Input:
ARR = [4, 3, 5, 2, 3, 1, 2]
Output:
[4, 3, 5, 2, 3, 1, 2]
Explanation:
The array can be arranged in a wave pattern as follows:
4 > 3
and3 < 5
5 > 2
and2 < 3
3 > 1
and1 < 2
Follow-up:
Try to achieve the solution with linear time complexity.
Input:
The input begins with an integer T
indicating the number of test cases.
Each of the following 2*T
lines specifies one test case:
- First line: a single integer N
, the number of elements in ARR
.
- Second line: N
space-separated integers representing ARR
.
Output:
For each test case, print the wave array as space-separated integers in a single line.
Constraints:
1 <= T <= 50
1 <= N <= 10^4
-10^9 <= ARR[i] <= 10^9
Note:
No need to explicitly print the output. The function implementation will handle it.

AnswerBot
1y
The task is to sort an array in a wave form, where each element is greater than or equal to its adjacent elements.
Iterate through the array and swap adjacent elements if they do not follow the wave pa...read more
Help your peers!
Add answer anonymously...
Josh Technology Group Software Developer interview questions & answers
A Software Developer was asked 10mo agoQ. What is an API and how does it work?
A Software Developer was asked 10mo agoQ. What is the software development life cycle, and what steps does it involve?
A Software Developer was asked Q. Given a value, find the next sibling on the right side of it without using globa...read more
Popular interview questions of Software Developer
A Software Developer was asked 10mo agoQ1. What is an API and how does it work?
A Software Developer was asked 10mo agoQ2. What is the software development life cycle, and what steps does it involve?
A Software Developer was asked Q3. Given a value, find the next sibling on the right side of it without using globa...read more
>
Josh Technology Group Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

