Rearrange array such that even positioned are greater than odd
Submissions: 2659   Accuracy:

34.09%

  Difficulty: Easy   Marks: 2

Given an array A of n elements, rearrange the array according to the following relations :
 A[i] >= A[i-1] , if i is even.
 A[i] <= A[i-1] , if i is odd.[Considering 1-indexed array]
Print the resultant array.

Input:

The first line of the input contains an integer T, denoting number of test cases. The first line of each test case contains an integer N denoting the size of the array. The second line of each test cases N space separated integers.
Output:

For each test case, print rearranged array.
Constraints:

1<=T<=100

1<=N<=100

1<=arr<=100
Example:

Input:

2

4

1 2 2 1

3

1 3 2

Output:

1 2 1 2

1 3 2

Explanation:

Testcase 1:

Both 2 are at even positions and 1 at odd satisfying given condition

Testcase 2:

Here, the array is already sorted as per the conditions.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sahil Chhabra
Author: Sahil_Chhabra


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.