Given an array of digits digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. If there is no answer return an empty string.
Since the answer may not fit in an integer data type, return the answer as a string. Note that the returning answer must not contain 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: ""
Constraints:
1 <= digits.length <= 1040 <= digits[i] <= 9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding the largest multiple of three from a set of digits involves considering every possible combination of digits. We essentially try building all possible numbers using different selections from the input digits. Then, we check each constructed number to see if it's divisible by three and keep track of the largest one we find.
Here's how the algorithm would work step-by-step:
def largest_multiple_of_three_brute_force(digits):
largest_multiple = ""
def generate_combinations(current_combination, remaining_digits):
nonlocal largest_multiple
# Convert the current combination to an integer and check divisibility.
if current_combination:
number = int("".join(map(str, sorted(current_combination, reverse=True))))
if number % 3 == 0:
if largest_multiple == "" or number > int(largest_multiple):
largest_multiple = str(number)
# If we've run out of digits, stop.
if not remaining_digits:
return
# Iterate through the remaining digits to build combinations.
for i in range(len(remaining_digits)):
# Recursively generate combinations by including each digit.
generate_combinations(current_combination + [remaining_digits[i]], remaining_digits[:i] + remaining_digits[i+1:])
generate_combinations([], digits)
if largest_multiple == "":
return ""
# Handle leading zeros, like "000" should be "0".
if all(digit == '0' for digit in largest_multiple):
return "0"
return largest_multipleTo find the largest multiple of three that can be formed from a set of digits, we focus on building the largest number possible and then adjusting to make it divisible by three. The core idea is to remove the smallest number of digits to achieve divisibility by three while keeping the remaining digits as large as possible.
Here's how the algorithm would work step-by-step:
def largestMultipleOfThree(digits):
sum_of_digits = sum(digits)
# If the sum is divisible by three, simply sort and return
if sum_of_digits % 3 == 0:
digits.sort(reverse=True)
result = ''.join(map(str, digits))
return '0' if result == '0' * len(result) else result
remainder = sum_of_digits % 3
# Function to remove digits based on remainder
def remove_digits(remainder_to_remove):
group_one = []
group_two = []
for digit in sorted(digits):
if digit % 3 == 1:
group_one.append(digit)
elif digit % 3 == 2:
group_two.append(digit)
if remainder_to_remove == 1:
if group_one:
digits.remove(group_one[0])
return True
elif len(group_two) >= 2:
digits.remove(group_two[0])
digits.remove(group_two[1])
return True
else:
if group_two:
digits.remove(group_two[0])
return True
elif len(group_one) >= 2:
digits.remove(group_one[0])
digits.remove(group_one[1])
return True
return False
# Remove the necessary digits
if not remove_digits(remainder):
return ''
digits.sort(reverse=True)
result = ''.join(map(str, digits))
# Handle all zeros case
if result == '0' * len(result):
return '0'
return result| Case | How to Handle |
|---|---|
| Empty input list | Return an empty string as there is no multiple of three that can be formed. |
| List contains only zeros | Return '0' as this is the largest multiple of three. |
| List contains digits that sum to a value not divisible by 3, but removing one or two smallest digits results in a divisible sum | Iteratively remove the smallest 1 or 2 digits that give remainder 1 or 2 mod 3. |
| List contains only digits that sum to a value not divisible by 3, and no removal of digits can make it divisible by 3. | Return an empty string since no multiple of 3 can be formed. |
| List contains a very large number of digits. | The solution should be designed to avoid integer overflow when calculating the sum and avoid creating too many copies of sublists. |
| Input list contains leading zeros after processing. | Remove leading zeros from the resulting string, unless the entire string becomes empty, in which case it should be '0'. |
| Input digits are already sorted in descending order. | The sorting step should not cause unexpected behavior and should still lead to the correct largest multiple of 3. |
| Input contains large repeating digit sequences | The remainder calculation after sorting should work correctly, even with repeating sequences. |