Introduction
The Largest Number problem involves arranging numbers such that they form the largest possible number.
Given:
- an array of non-negative integers
the task is to:
- arrange numbers
- concatenate them
- return the largest possible number
This problem is one of the most important applications of:
Custom Sorting ComparatorThis problem helps in understanding:
- custom sorting
- string comparison
- greedy ordering
- comparator logic
Example
Input:arr = [3, 30, 34, 5, 9]
Output:
"9534330"
Explanation:
Correct arrangement:
9 → 5 → 34 → 3 → 30
Largest number formed:
9534330
Constraints
1 <= arr.length <= 10^50 <= arr[i] <= 10^9Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Generate all permutations
- Form numbers using concatenation
- Track largest value
This works for small arrays but becomes extremely slow for large inputs.
Steps
- Generate all permutations.
- Concatenate numbers.
- Compare generated values.
- Store maximum number.
- Return largest result.
Dry Run
Input:[3, 30, 34]
Possible permutations:
33034
33430
30334
30343
34330
34303
Largest number:
34330
Final Result:
"34330"
Brute Force Code
Complexity Analysis
Time Complexity: O(n!)Explanation:
All permutations are generated.
Space Complexity: O(n)
Explanation:
Recursive stack and permutations are used.
Approach 2 : Custom Comparator Sorting
Explanation
The optimized solution uses:
Custom ComparatorThe idea is:
For two numbers:
- x
- y
compare:
xy vs yxExample:
"9" + "34" = "934""34" + "9" = "349"Since:
934 > 3499 should come before 34.
Steps
- Convert numbers to strings.
- Apply custom sorting.
- Compare:
- xy
- yx
- Arrange largest combinations first.
- Join strings.
- Return result.
Dry Run
Input:[3, 30, 34, 5, 9]
Compare: 3 and 30 330 > 303
So: 3 comes before 30
Compare: 9 and 34
934 > 349
So: 9 comes before 34
Sorted order: [9, 5, 34, 3, 30]
Final Result:
"9534330"
Custom Comparator Sorting Code
Complexity Analysis
Time Complexity: O(n log n)Explanation:
Custom sorting dominates the complexity.
Space Complexity: O(n)
Explanation:
Extra string storage is used.
Edge Cases
- All zeros
- Single element array
- Large numbers
- Duplicate numbers
- Numbers with same prefixes
Why This Problem is Important
Largest Number helps in understanding:
- Custom comparators
- String-based sorting
- Greedy ordering
- Comparator logic
- Sorting applications
It is one of the most important custom sorting interview problems.
Real-World Applications
Custom sorting concepts are used in:
- Ranking systems
- Priority ordering
- Search engines
- Recommendation systems
- Scheduling systems
Common Mistakes
- Incorrect comparator logic
- Forgetting string conversion
- Handling leading zeros incorrectly
- Wrong concatenation order
Interview Tips
Interviewers often expect:
- Custom comparator explanation
- xy vs yx comparison logic
- Sorting optimization understanding
Always explain:
- why normal sorting fails
- why concatenation comparison works
- how greedy ordering forms maximum number
Related Questions
- Merge Intervals
- Quick Sort
- Sort Colors
- Partition Array
- Kth Largest Element
Final Takeaway
The Largest Number problem is a fundamental custom sorting problem that teaches comparator-based ordering and greedy arrangement techniques. Understanding this problem builds a strong foundation for advanced sorting and comparator interview problems.