#K9136. Form the Largest Number

    ID: 37958 Type: Default 1000ms 256MiB

Form the Largest Number

Form the Largest Number

You are given a list of non-negative integers. Your task is to arrange them in such a way that they form the largest possible number. The answer may be very large, so you must return a string instead of an integer.

More formally, given an array of integers \(nums\), you need to form a string by concatenating the integers in some order such that the resulting string is the largest possible numerical value. If the resulting number starts with one or more zeros, it should be reduced to a single '0'.

Example:

  • For \(nums = [10, 2, 9]\), the largest arrangement is "9210".
  • For \(nums = [54, 546, 548, 60]\), the largest arrangement is "6054854654".

Hints: This problem requires a custom comparison strategy. Consider the concatenation of two numbers \(x\) and \(y\) as strings, i.e., compare \(xy\) with \(yx\) to determine which ordering forms a larger number.

inputFormat

The input is given from standard input. The first line contains an integer \(T\) representing the number of test cases. For each test case, the first line contains an integer \(n\) (the number of elements) followed by a line of \(n\) space-separated non-negative integers.

For example:

3
3
10 2 9
4
54 546 548 60
5
1 2 3 4 5

outputFormat

For each test case, output the largest number formed by concatenating the integers, each on a new line, to the standard output.

For the sample input, the expected output is:

9210
6054854654
54321
## sample
1
3
10 2 9
9210