ARRANGE - Arranging Amplifiers

Scientists at the TIFR, Mumbai, are doing some cutting edge research on the Propagation of Signals. A young researcher comes up with a method of progressively amplifying signals, as they progress along a path. The method involves the placing of Amplifiers at regular distances along the line. Each amplifier is loaded with a number a(i), which is called its amplification factor. The method of amplification is simple: an amplifier which recieves a signal of strength X, and has Y loaded in it, results in a signal of strength Y^X [ Y to the power X]. In course of his research, the young scientist tries to find out, that given a set of n amplifiers loaded with a(0), a(1), a(2), ...., a(n-1), which particular permutation of these amplifiers, when placed at successive nodes, with the initial node given a signal of strength 1, produces the strongest output signal.

this is better illustrated by the following example : 5 6 4
4^(5^(6^1)) is the strength of the strongest signal, which is generated by putting amplifier loaded with 6 in first place, 5 in second place and 4 in third place.

Given a list of integers specifying the set of amplifiers at hand, you must find out the order in which they must be placed, to get the highest signal strength. In case their exist multiple permutations with same output, you should print the one which has bigger amplifiers first.


First line of input contains T, the number of test cases. For each test case first line contains a number ni, which is equal to the number of amplifiers available. Next line contains n integers, separated by spaces which denote the values with which the amplifiers are loaded.


Output contains T lines, one for each test case. Each line contains ni integers, denoting the order in which the amplifiers should be kept such that the result is strongest.


5 6 4
2 3

6 5 4
2 3

Constraints and Limits

T ≤ 20, Ni ≤ 10^5.
Each amplifier will be loaded with a positive integer p, 0 < p ≤ 10^9.
No two amplifier > 1 shall be loaded with the same integer.

hide comments
ashgreninja43: 2020-09-18 06:39:34

I think for 2 and 4 too we need to print 2 4

aditya2020: 2020-06-10 12:00:40

somebody pls help me. I've taken care of 2,3 issue. I'm printing all 1's then reverse sorted the entire array and in the end printing 2 or 3 as per existence. Pls share your correct solution or tell me where I could go wrong

anirudh_1999: 2020-04-04 22:52:52

why is it giving tle in java?

wingman__7: 2019-06-07 09:24:40

take care of test case where all elements of the input array are 1. It costed me 1 WA

kamesh11: 2019-03-14 18:24:39

Constraints are the key to this problem! Costed me 4 WA's as I neglected the constraints.

tilara: 2018-09-01 14:15:20

no repetition of numbers other than 1 easy .big hint: just check for 2 and 3

anshuman1117: 2018-08-15 09:08:09

The test cases containing only 2 & 3, with of course any number of 1s will be problematic. Check for them specially. For other cases, just sort and print. Also, take care of the 1s. There can be more than one 1s in the test cases.

Happy coding, happy learning :)

Jasbir Singh: 2018-04-02 23:45:32

"No two amplifier > 1 shall be loaded with the same integer." Costed me 4WA's . 1 can be repeated.

piyush_682: 2017-06-23 12:49:25

pretty easy problem, AC in one go!

Last edit: 2017-06-23 12:57:21
siddharth_0196: 2016-10-15 12:54:47

Take care of the test cases having elements 2 and 3! ;)

Last edit: 2016-10-15 12:58:38

Added by:Ajay Somani
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:CodeCraft 08 Anshuman Singh