Zig-Zag Array Rearrangement
You are provided with an array of distinct elements, and your task is to rearrange the array elements in a zig-zag manner. Specifically, for every odd index i
, the element ARR[i]
should be greater than both ARR[i-1]
and ARR[i+1]
.
Input:
The first line contains an integer ‘T’ which represents the number of test cases.
Each test case starts with an integer ‘N’ representing the size of the array.
The next line contains ‘N’ space-separated integers denoting the array elements.
Output:
A zig-zag array for each test case. The output function will validate if the returned array is in zig-zag fashion.
Example:
Input:
N = 4, ARR = [4, 3, 2, 1]
Output:
A possible zig-zag array: [3, 4, 1, 2]
Note:
It is guaranteed that a zig-zag rearrangement is always possible for the given array. Multiple correct answers may exist.
Constraints:
1 <= T <= 10
1 <= N <= 5*103
0 <= ARR[i] <= 106
- Time Limit: 1 sec

AnswerBot
4mo
Rearrange array elements in a zig-zag manner where every odd index element is greater than its neighbors.
Iterate through the array and swap elements at odd indices to satisfy the zig-zag condition.
Ens...read more
Help your peers!
Add answer anonymously...
Josh Technology Group Software Developer Intern interview questions & answers
A Software Developer Intern was asked 3mo agoQ. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked 3mo agoQ1. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ2. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q3. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
>
Josh Technology Group Software Developer Intern 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

