Largest Number - Problem

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.

Since the result may be very large, you need to return a string instead of an integer.

Example: Given [10,2], return "210" (not "102")

Input & Output

Example 1 — Basic Case
$ Input: nums = [10,2]
Output: "210"
💡 Note: Compare arrangements: [10,2] gives "102" vs [2,10] gives "210". Since "210" > "102", return "210".
Example 2 — Multiple Numbers
$ Input: nums = [3,30,34,5,9]
Output: "9534330"
💡 Note: Custom sort compares concatenations: "9"+"5" > "5"+"9", "34"+"3" > "3"+"34", "3"+"30" > "30"+"3", resulting in [9,5,34,3,30] → "9534330".
Example 3 — Edge Case with Zeros
$ Input: nums = [0,0]
Output: "0"
💡 Note: All numbers are zero, so result is "0" (not "00").

Constraints

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Largest Number: Arrange for Maximum Result102Input: [10, 2]Two possible arrangements:"102""210"[10, 2][2, 10]Output: "210" (largest possible)Compare arrangements and select maximum
Understanding the Visualization
1
Input
Array of non-negative integers [10, 2]
2
Process
Compare all arrangements to find largest
3
Output
Return largest number as string "210"
Key Takeaway
🎯 Key Insight: Compare concatenated results (xy vs yx) to determine optimal ordering
Asked in
Google 25 Amazon 20 Microsoft 15
185.0K Views
Medium Frequency
~25 min Avg. Time
3.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen