Largest Number

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Largest Number

Approach

Convert numbers to strings. Sort with a custom comparator that compares the concatenation a+b vs b+a. If a+b is larger, a should come first. Join the sorted strings. Handle edge case where all numbers are 0.

Complexity Analysis

Time
O(n log n)
Space
O(n)

Pattern

Custom Comparator

Why It Works

The comparator defines a total ordering: if ab > ba, then a should precede b. This greedy choice is provably optimal because it maximizes the contribution of each number in its position. The edge case check for leading zeros handles inputs like [0, 0].

Updated Feb 2026