Skills101
← All coding problems

Largest Number From an Array

mediumPlacement Coding+20 XPTime limit 2s

Arrange the numbers side by side, in any order, to form the largest possible number. For [3, 30, 34, 5, 9] the answer is 9534330.

Input:

  • line 1: n
  • line 2: n non-negative integers

Output: the largest number, as a string. If the result is all zeros, print a single 0.

Constraints: 1 ≤ n ≤ 10⁴, 0 ≤ values ≤ 10⁹.

Think about it: sorting by value fails (9 must come before 30). Compare two numbers x and y as strings: x goes first if x + y > y + x. Sort with that rule and join.

Sample input 1

5
3 30 34 5 9

Sample output 1

9534330

Sample input 2

2
10 2

Sample output 2

210

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.