Maximize $a_1^{a_2^{\ldots^{a_n}}}$, where $(a_1,a_2,\ldots,a_n)$ is a permutation of $(b_1,b_2,\ldots,b_n)$

89 Views Asked by At

You are given a tuple of integers $B=(b_1,b_2,\ldots,b_n)$.

Find $(a_1,a_2,\ldots,a_n)$ - a permutation of $(b_1,b_2,\ldots,b_n)$ - that maximizes $a_1^{a_2^{\ldots^{a_n}}}$.

For example -

If $B=(5, 6, 4)$, then the answer is $(4 ,5, 6)$.

If $B=(2,3)$, then the answer is $(3,2)$.

Note - Power is calculated in order given below

$a_1^{(a_2^{(\ldots^{a_n})})}$.

1

There are 1 best solutions below

0
On BEST ANSWER

Sharing the answer for audience-

  1. Sort the numbers $b_1,b_2,\ldots,b_n$ in decreasing order.Lets call it Seq.
  2. Let answer be A. All the 1s to A and remove them from Seq. i.e If Seq is 4,3,2,1,1 then after step 2 seq will be 2,3,4 and A = 1,1

  3. if first element in Seq is 3 and second element is 2 then remove both from Seq and add 2,3 to A

  4. Add rest of the elements from Seq to A.

  5. Reverse the numbers in A

Let me know if you find anything wrong in given solution.