Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-289

Largest Multiple of Three

Problem

Given a list of digits, return the largest number divisible by 3 that can be formed using some or all of the digits, as a string without leading zeros, or the empty string if impossible.

Count digit frequencies and remove the fewest digits needed to make the digit sum divisible by 3, then arrange the rest in descending order. All zeros produce the string 0.

Hint

For a remainder of 1, either drop one digit with remainder 1 or two digits with remainder 2.

Test cases

case 1
in: [[8,1,9]]
expected: "981"
case 2
in: [[8,6,7,1,0]]
expected: "8760"
case 3
in: [[1]]
expected: ""
case 4
in: [[0,0,0]]
expected: "0"
case 5
in: [[1,2,3]]
expected: "321"
Starter codePython
def largest_multiple_of_three(digits):
    # Your code here
    pass

5 test cases · run your code against them in the editor