Program to find hoe many children will get candies while distributing them maintaining the rules in Python

Suppose we have k number of candies to distribute among children following specific rules. This problem requires finding the maximum number of children who can receive candies while maintaining distribution constraints.

Distribution Rules

  • The ith child will get number of candies
  • Children must be served in order − child at index i cannot get candies until all children from index 1 to i−1 are served
  • If the ith child cannot get exactly candies, the distribution stops

Example Walkthrough

If k = 20, the distribution works as follows ?

  • 1st child gets 1² = 1 candy (remaining: 19)
  • 2nd child gets 2² = 4 candies (remaining: 15)
  • 3rd child gets 3² = 9 candies (remaining: 6)
  • 4th child needs 4² = 16 candies, but only 6 remain − invalid distribution

Therefore, only 3 children can be served.

Mathematical Approach

The total candies needed for n children is the sum of squares: 1² + 2² + 3² + ... + n². This equals the formula n(n+1)(2n+1)/6.

We use binary search to efficiently find the maximum number of children ?

def solve(k):
    left = 0
    right = k
    
    while right - left > 1:
        mid = (left + right) // 2
        # Calculate sum of squares from 1 to mid
        total_candies_needed = mid * (mid + 1) * (2 * mid + 1) // 6
        
        if total_candies_needed > k:
            right = mid
        else:
            left = mid
    
    # Check if 'right' number of children can be served
    if right * (right + 1) * (2 * right + 1) <= k * 6:
        return right
    return left

# Test with example
k = 20
result = solve(k)
print(f"Number of children who can get candies: {result}")

# Verify the calculation
total_used = sum(i**2 for i in range(1, result + 1))
print(f"Total candies used: {total_used}")
print(f"Remaining candies: {k - total_used}")
Number of children who can get candies: 3
Total candies used: 14
Remaining candies: 6

How the Algorithm Works

The binary search approach works by ?

  • Left boundary: Starts at 0 (minimum children)
  • Right boundary: Starts at k (maximum possible children)
  • Middle calculation: Tests if mid number of children can be served
  • Convergence: Narrows down to the exact maximum number

Alternative Simple Approach

For smaller values of k, you can use a simple iterative approach ?

def solve_simple(k):
    children = 0
    candies_used = 0
    
    while True:
        children += 1
        candies_needed = children ** 2
        
        if candies_used + candies_needed <= k:
            candies_used += candies_needed
        else:
            return children - 1
    
# Test both approaches
k = 20
print(f"Binary search result: {solve(k)}")
print(f"Simple approach result: {solve_simple(k)}")
Binary search result: 3
Simple approach result: 3

Conclusion

The binary search approach efficiently finds the maximum number of children who can receive candies using the sum of squares formula. For large values of k, this method is much faster than iterating through each child sequentially.

Updated on: 2026-03-26T18:28:38+05:30

371 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements