Watch 10 video solutions for Maximum Number of Balls in a Box, a easy level problem involving Hash Table, Math, Counting. This walkthrough by Programming Live with Larry has 1,578 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: lowLimit = 1, highLimit = 10 Output: 2 Explanation: Box Number: 1 2 3 4 5 6 7 8 9 10 11 ... Ball Count: 2 1 1 1 1 1 1 1 1 0 0 ... Box 1 has the most number of balls with 2 balls.
Example 2:
Input: lowLimit = 5, highLimit = 15 Output: 2 Explanation: Box Number: 1 2 3 4 5 6 7 8 9 10 11 ... Ball Count: 1 1 1 1 2 2 1 1 1 0 0 ... Boxes 5 and 6 have the most number of balls with 2 balls in each.
Example 3:
Input: lowLimit = 19, highLimit = 28 Output: 2 Explanation: Box Number: 1 2 3 4 5 6 7 8 9 10 11 12 ... Ball Count: 0 1 1 1 1 1 1 1 1 2 0 0 ... Box 10 has the most number of balls with 2 balls.
Constraints:
1 <= lowLimit <= highLimit <= 105Problem Overview: You are given a range of ball numbers from lowLimit to highLimit. Each ball goes into a box identified by the sum of its digits. For example, ball 321 goes to box 3 + 2 + 1 = 6. After placing every ball, the task is to return the maximum number of balls in any single box.
The key observation: the box number is simply the digit sum of the ball number. So the problem reduces to counting how many times each digit sum appears while iterating through the range.
Approach 1: Counting with Frequency Map (Time: O(n * d), Space: O(k))
Iterate from lowLimit to highLimit. For each number, compute its digit sum by repeatedly extracting digits with num % 10 and dividing by 10. Store the count of each box using a hash map where the key is the digit sum and the value is the number of balls in that box. After processing every number, return the maximum value in the map. This approach uses a hash table for flexible counting and works well when the possible digit sums are not tightly bounded.
The runtime is O(n * d), where n is the number of balls and d is the number of digits per number (at most 5 for this problem). Space complexity is O(k), where k is the number of unique digit sums.
Approach 2: Optimized Counting with Array (Time: O(n * d), Space: O(1))
Digit sums have a small upper bound. For example, the maximum possible sum for a five-digit number is 9 + 9 + 9 + 9 + 9 = 45. Instead of a hash map, use a fixed-size array where the index represents the digit sum and the value stores the ball count. Iterate through the range, compute the digit sum, and increment the corresponding index.
This converts the frequency structure from a dynamic map to a simple array lookup, which is faster in practice due to constant-time indexing and lower overhead. The algorithm still performs digit extraction using simple math operations and uses a counting technique to track frequencies.
The time complexity remains O(n * d), but space becomes effectively O(1) because the array size is constant (around 46 possible sums).
Recommended for interviews: The array-based counting solution is what interviewers typically expect. It shows you recognized that the digit sum range is small and replaced the hash map with a constant-size frequency array. Starting with the map approach demonstrates correct reasoning, while the optimized array version shows you can reduce overhead by analyzing constraints.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counting with Frequency Map | O(n * d) | O(k) | General solution when the range of keys is unknown or large |
| Optimized Counting with Array | O(n * d) | O(1) | Best choice when digit sum range is small and predictable |