Largest Multiple of Three - Problem
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.
Note: The answer may not fit in an integer data type, so return the answer as a string. The returning answer must not contain unnecessary leading zeros.
Input & Output
Example 1 — Basic Case
$
Input:
digits = [8,1,9]
›
Output:
981
💡 Note:
Sum = 8+1+9 = 18, which is divisible by 3. Use all digits arranged in descending order to get the largest number: 981.
Example 2 — Need to Remove Digits
$
Input:
digits = [8,6,7,1,0]
›
Output:
8760
💡 Note:
Sum = 22, remainder = 1. Remove the smallest digit with remainder 1 (which is 1). Remaining digits [8,6,7,0] sum to 21, divisible by 3. Result: 8760.
Example 3 — All Zeros Case
$
Input:
digits = [0,0,0]
›
Output:
0
💡 Note:
Sum = 0, divisible by 3. All digits are zero, so return single '0' to avoid leading zeros.
Constraints
- 1 ≤ digits.length ≤ 104
- 0 ≤ digits[i] ≤ 9
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of digits [8,1,9]
2
Process
Check sum divisibility by 3, arrange optimally
3
Output
Largest multiple of 3: 981
Key Takeaway
🎯 Key Insight: Use the divisibility rule for 3 (sum of digits) combined with greedy digit arrangement to construct the optimal solution efficiently.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code