Timer is Running

Reaching the heights
Submissions: 1758   Accuracy:


  Difficulty: Easy   Marks: 2

Teacher gives a mental ability question to Raju . The question is as follows:-

Raju is in an elevator. Given by his teacher is an array of size N which denotes the number of floors and has a 1 based indexing. The elevator starts from the ground and moves up and down, X and Y floors respectively. There is a code used in the elevator according to which it moves up X floors given at odd indexes of the array and moves down Y floors given at even indexes of the array. He is asked to go to the highest floor possible. Help him to sort the array such that he reaches the highest floor after traversing the whole array from starting till the end, without skipping any index.

He always prefers to move more number of floors up and less number of floors down. Once he gets into the elevator, the elevator should not reach the ground again, if it does print “Not Possible”.


The first line of input contains an integer T denoting the number of test cases. T test cases follow. The first line of each test case contains N denoting size of the array. The second line of each test case contains elements of the array separated by a single space.


For each test case, print the sorted array or “Not Possible”.


1 ≤ T ≤ 10

1 ≤ N ≤ 1000

1 ≤ X , Y ≤ 1000





2 3 4 5


1 1


5 2 4 3

Not Possible


Case 1:

Array can be arranged as {5,3,4,2} or {4,3,5,2} or {4,2,5,3} but it will get arranged as {5,2,4,3} because he always prefer to move more number of floors up and less number of floors down.

Case 2:

Not possible.

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

Author: kartikeyagoswami20

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.