#1363

Largest Multiple of Three

international master · 1905 · lc hard +32 · verified · 33.1% accepted · 628 likes · top 11%

Description

Given an array of single digits digits, form the largest number that is a multiple of three by concatenating any subset of those digits in any order. Return this number as a string, or an empty string if no valid multiple of three can be constructed. The result must not have unnecessary leading zeros.

Example 1:

Input: digits = [8,1,9]
Output: "981"

Example 2:

Input: digits = [8,6,7,1,0]
Output: "8760"

Example 3:

Input: digits = [1]
Output: ""

Code

1
2
3