FastPrepLargest Number by Concatenation

Largest Number by Concatenation

NVIDIA logoNVIDIA● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Given a nonempty array nums of nonnegative integers, choose an order for all its items so that concatenating their decimal representations produces the largest possible number.

Use each occurrence exactly once. Return the concatenation as a string because it may exceed a fixed-width integer. If all items are zero, return "0".

Function

largestNumber(nums: int[]) → String

Examples

Example 1

nums = [3,54,5]return = "5543"

The reported example orders the numbers as 5, 54, then 3.

Example 2

nums = [10,2]return = "210"

Placing 2 before 10 gives 210 rather than 102.

Example 3

nums = [3,30,34,5,9]return = "9534330"

The pairwise concatenation order is 9, 5, 34, 3, 30.

Constraints

  • 1 <= nums.length <= 100.
  • 0 <= nums[i] <= 10^9.

More NVIDIA problems

See NVIDIA hiring insights
public String largestNumber(int[] nums) {
    return "";
}
nums[3,54,5]
expected"5543"
Checking account…