Given a collection of non-negative integers, arrange them so that gluing their decimal representations together produces the largest possible number. Return the result as a string, because it can be far too long to fit in a normal integer.
Watch out for the all-zero case: if every value is zero the answer should be a single "0" rather than a run of zeros.
Example 1
Input: nums = [3,30,34,5,9]
Output: "9534330"
Ordering the pieces as 9, 5, 34, 3, 30 yields the biggest concatenation.
Example 2
Input: nums = [10,2]
Output: "210"
Constraints
1 ≤ nums.length ≤ 1000 ≤ nums[i] ≤ 10^9See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI