


Let's say the given array is [ 9, 98, 7].
All the possible numbers formed by concatenating each of the array elements are 7989,7998,9879,9897,9987,9798. Among the six numbers, 9987 is the greatest number. Hence the answer is 9987.
The first line of input contains an integer ‘T’ denoting the number of test cases.
Then the T test cases follow.
The first line of each test case contains an integer 'N' denoting the number of elements in the array.
The second line of each test case contains 'N' space-separated integers denoting the array elements.
For each test case, print the largest number possible formed by concatenating each of the array elements exactly once.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1<= T <=10
1<= N <=10^3
0<= A[i] <=10^9
Time Limit: 1sec
The idea is to try out all the possible answers one by one and find out the largest one among all of them.
To implement this approach, we will generate all the permutations of the first 'N' positive integers and concatenate all the array elements in the order of the permutation.
For e.g. N=3 and array A = [ 3, 34, 7 ].
Let's generate all the possible answers using the permutations. Let "a=> b" denote the concatenation of integer a with b.
As we can see the largest number among the above is 7343. Hence, 7343 will be our answer.
Steps:
The idea is to build an auxiliary array in which all the array elements should have the same number of digits so that we can simply sort the auxiliary array in descending order and concatenate the array elements in that particular order to form the largest number possible.
The process required to build the auxiliary array is explained below:
See the image for a better understanding
After building the auxiliary array we will sort it in descending order then concatenate all the corresponding array elements in the order of the auxiliary elements to form the greatest number possible.
To solve this problem is to sort the array in descending order, but the sorting list/array does not work here. For example, 213 is greater than 75 but in a sorted array, 75 comes after 213. And this would produce the number 21375. But the largest number that can be formed is 75213. therefore sorting will not work here. But we can modify the comparator function of sort().
The idea is to use any of the comparison-based sorting algorithm
For Example A = 213 and B = 75 then AB = 21375 and BA = 75213.To compare A and B our custom compare function will compare AB and BA, Since AB < BA so, we will put B first in sorted list/array.