Distribute Candies to People in Python

The candy distribution problem involves giving candies to people in increasing amounts across multiple rounds. In the first round, person 1 gets 1 candy, person 2 gets 2 candies, and so on. In subsequent rounds, the candy count continues incrementally from where it left off.

Problem Understanding

Given candies total candies and n people, we distribute candies in rounds ?

  • Round 1: Give 1, 2, 3, ..., n candies
  • Round 2: Give n+1, n+2, n+3, ..., 2n candies
  • Continue until candies run out
  • The last person gets remaining candies (if any)

Algorithm Steps

  • Initialize result array with zeros for n people
  • Use index to track current candy count (starts at 1)
  • Use position to cycle through people (0 to n-1)
  • Give minimum of remaining candies or current candy count
  • Update candies and increment counters

Implementation

def distribute_candies(candies, num_people):
    result = [0] * num_people
    candy_count = 1
    position = 0
    
    while candies > 0:
        # Give min of remaining candies or current candy count
        give = min(candies, candy_count)
        result[position] += give
        
        # Update remaining candies and counters
        candies -= give
        candy_count += 1
        position = (position + 1) % num_people
    
    return result

# Test the function
print(distribute_candies(7, 3))
print(distribute_candies(10, 3))
[3, 2, 2]
[5, 2, 3]

Step-by-Step Example

For candies = 7 and num_people = 3 ?

def distribute_candies_verbose(candies, num_people):
    result = [0] * num_people
    candy_count = 1
    position = 0
    
    print(f"Initial: {result}, candies left: {candies}")
    
    while candies > 0:
        give = min(candies, candy_count)
        result[position] += give
        candies -= give
        
        print(f"Person {position + 1} gets {give} candies")
        print(f"Current state: {result}, candies left: {candies}")
        
        candy_count += 1
        position = (position + 1) % num_people
    
    return result

distribute_candies_verbose(7, 3)
Initial: [0, 0, 0], candies left: 7
Person 1 gets 1 candies
Current state: [1, 0, 0], candies left: 6
Person 2 gets 2 candies
Current state: [1, 2, 0], candies left: 4
Person 3 gets 3 candies
Current state: [1, 2, 3], candies left: 1
Person 1 gets 1 candies
Current state: [2, 2, 3], candies left: 0
[2, 2, 3]

Alternative Approach Using Math

For large numbers, we can calculate complete rounds mathematically ?

import math

def distribute_candies_optimized(candies, num_people):
    result = [0] * num_people
    
    # Calculate complete rounds
    complete_rounds = int((-1 + math.sqrt(1 + 8 * candies / num_people)) / 2)
    
    # Distribute candies for complete rounds
    for i in range(num_people):
        result[i] = complete_rounds * (i + 1) + complete_rounds * (complete_rounds - 1) * num_people // 2
    
    # Handle remaining candies
    remaining = candies - complete_rounds * (complete_rounds + 1) * num_people // 2
    candy_count = complete_rounds * num_people + 1
    
    for i in range(num_people):
        if remaining > 0:
            give = min(remaining, candy_count)
            result[i] += give
            remaining -= give
            candy_count += 1
    
    return result

print(distribute_candies_optimized(10, 3))
[5, 2, 3]

Conclusion

The candy distribution problem can be solved by simulating the process or using mathematical optimization. The simple approach iterates through each candy distribution, while the optimized version calculates complete rounds mathematically for better performance with large inputs.

Updated on: 2026-03-25T07:22:04+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements