Program to find maximum number of balls in a box using Python

Suppose we have a ball factory where we have n balls numbered from l to r (both inclusive) and have an infinite number of boxes numbered from 1 to infinity. We put each ball in the box with a number equal to the sum of digits of the ball's number. For example, ball number 123 will be put in box number 1 + 2 + 3 = 6. Given two values l and r, we need to find the maximum number of balls in any single box.

Example

If the input is l = 15 and r = 25, let's see which box each ball goes into ?

  • Ball 15 goes to box 1+5 = 6

  • Ball 16 goes to box 1+6 = 7

  • Ball 17 goes to box 1+7 = 8

  • Ball 18 goes to box 1+8 = 9

  • Ball 19 goes to box 1+9 = 10

  • Ball 20 goes to box 2+0 = 2

  • Ball 21 goes to box 2+1 = 3

  • Ball 22 goes to box 2+2 = 4

  • Ball 23 goes to box 2+3 = 5

  • Ball 24 goes to box 2+4 = 6

  • Ball 25 goes to box 2+5 = 7

Box 6 contains balls [15, 24] = 2 balls, and box 7 contains balls [16, 25] = 2 balls. The maximum count is 2.

Algorithm

To solve this problem, we follow these steps ?

  • Create a dictionary to count balls in each box

  • For each ball number from l to r:

    • Calculate the sum of its digits

    • Increment the count for that box number

  • Return the maximum count from all boxes

Implementation

def solve(l, r):
    box_count = {}
    
    for i in range(l, r + 1):
        # Calculate sum of digits
        digit_sum = 0
        for digit in str(i):
            digit_sum += int(digit)
        
        # Count balls in each box
        if digit_sum not in box_count:
            box_count[digit_sum] = 0
        box_count[digit_sum] += 1
    
    # Return maximum count
    return max(box_count.values())

# Test with example
l = 15
r = 25
result = solve(l, r)
print(f"Maximum balls in any box: {result}")

# Let's also see the distribution
def solve_with_details(l, r):
    box_count = {}
    
    for i in range(l, r + 1):
        digit_sum = sum(int(digit) for digit in str(i))
        box_count[digit_sum] = box_count.get(digit_sum, 0) + 1
    
    print(f"Box distribution: {dict(sorted(box_count.items()))}")
    return max(box_count.values())

print("\nDetailed analysis:")
solve_with_details(15, 25)
Maximum balls in any box: 2

Detailed analysis:
Box distribution: {2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 2, 8: 1, 9: 1, 10: 1}

Optimized Version

Here's a more concise implementation using Python's built-in functions ?

def solve_optimized(l, r):
    from collections import Counter
    
    # Calculate digit sum for each ball and count occurrences
    digit_sums = [sum(int(digit) for digit in str(i)) for i in range(l, r + 1)]
    box_count = Counter(digit_sums)
    
    return max(box_count.values())

# Test
l, r = 15, 25
result = solve_optimized(l, r)
print(f"Maximum balls in any box: {result}")

# Test with another range
l2, r2 = 1, 20
result2 = solve_optimized(l2, r2)
print(f"For range 1-20, maximum balls: {result2}")
Maximum balls in any box: 2
For range 1-20, maximum balls: 2

How It Works

The algorithm works by:

  1. Digit Sum Calculation: For each ball number, we convert it to a string and sum all individual digits

  2. Box Assignment: The digit sum determines which box the ball goes into

  3. Counting: We maintain a count of balls in each box using a dictionary

  4. Maximum: Finally, we return the highest count among all boxes

Conclusion

This problem demonstrates how to use dictionaries for counting and the importance of digit manipulation in programming. The key insight is mapping each ball to its digit sum and finding the most populated box.

Updated on: 2026-03-25T20:34:29+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements