Maximum Number of Balls in a Box - Problem
You are working in a ball factory where you have n balls numbered from lowLimit up to highLimit inclusive (i.e., n == highLimit - lowLimit + 1), and an infinite number of boxes numbered from 1 to infinity.
Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball's number. For example, the ball number 321 will be put in the box number 3 + 2 + 1 = 6 and the ball number 10 will be put in the box number 1 + 0 = 1.
Given two integers lowLimit and highLimit, return the number of balls in the box with the most balls.
Input & Output
Example 1 — Small Range
$
Input:
lowLimit = 1, highLimit = 10
›
Output:
2
💡 Note:
Balls 1,10 both go to box 1 (digit sum 1). Balls 2-9 each go to different boxes. Box 1 has maximum 2 balls.
Example 2 — Sequential Range
$
Input:
lowLimit = 5, highLimit = 15
›
Output:
2
💡 Note:
Balls 5,14 both go to box 5 (5+0=5, 1+4=5). Ball 6→box6, 7→box7, etc. Box 5 has maximum 2 balls.
Example 3 — Same Digit Sum
$
Input:
lowLimit = 19, highLimit = 28
›
Output:
2
💡 Note:
Balls 19,28 both go to box 10 (1+9=10, 2+8=10). Most other balls go to unique boxes.
Constraints
- 1 ≤ lowLimit ≤ highLimit ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Range
Ball numbers from lowLimit to highLimit
2
Calculate Digit Sums
Each ball goes to box number equal to sum of its digits
3
Count and Find Max
Return the box with most balls
Key Takeaway
🎯 Key Insight: Group balls by digit sum and count - the largest group gives the answer
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code