Loading...
Loading...
At a college fest stall, each player gets n cards. Each card shows a whole number. The player lays all the cards in a row, side by side, and reads the digits as one big number. The biggest number wins a prize.
Print the largest number that can be made. The answer can be very long, so print it as text. It must not start with extra zeros: if every card is 0, print just 0.
Example 1
Input
5 3 30 34 5 9
Output
9534330
The best order is 9, 5, 34, 3, 30, which reads 9534330. Putting 30 before 3 would give the smaller 9534303.
Example 2
Input
2 10 2
Output
210
2 then 10 gives 210, which is bigger than 102.
Try the problem first. Open one hint at a time.
Normal number order does not work: 9 must come before 34 even though it is smaller.
For two cards a and b, the only question is which joined result is bigger: a then b, or b then a.
Turn the numbers into strings and sort them with that rule. Two joined strings of the same length can be compared with compareTo.
After sorting, if the first string is "0", every card is 0.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor