Largest number formed
Write a function that arranges a list of non-negative integers to form the largest possible number. Given an array of integers, rearrange them such that when concatenated, they produce the maximum possible numeric value.
Input
- An array
arrof non-negative integers.
Output
- A string representing the largest number that can be formed by arranging the given integers.
Constraints & Edge Cases
-
All integers in the array are non-negative.
-
The resulting number may be very large, so return it as a string.
-
If all numbers are 0, return
"0"(not"000"). -
The array may contain duplicate numbers.
-
When comparing numbers for arrangement, treat them as strings:
-
For example,
"30"should come after"3"because:"330">"303"→ so"3"should come before"30".
-
//Example 1: Input: const arr = [3, 30, 34, 5, 9] output: "9534330" //Example 2: Input: const arr3 = [54, 546, 548, 60] output: "6054854654"
Companies:
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
