Facebook Pixel

Larget number formed

JavaScript
easy
20 mins

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 arr of 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: "60548546654"

Companies:

amazon

Solve Similar questions 🔥