09 Sept 2026•Go / Python / TypeScript•Easy

Distribute Candies to People

Distribute increasing candy amounts cyclically until no candies remain.

Simulate each gift, cap it by the remaining candies, add it to the current cyclic recipient, and increase the next gift by one.

complexity

O(sqrt(candies)) time and O(people) output space.

solution files

  • Go distribute-candies-to-people/solution.go
  • Python distribute-candies-to-people/solution.py
  • TypeScript distribute-candies-to-people/solution.ts

Solution files

Go•distribute-candies-to-people/solution.go
package main

func distributeCandies(candies int, numPeople int) []int {
	result := make([]int, numPeople)
	gift, person := 1, 0
	for candies > 0 {
		amount := gift
		if candies < amount {
			amount = candies
		}
		result[person] += amount
		candies -= amount
		gift++
		person = (person + 1) % numPeople
	}
	return result
}
Python•distribute-candies-to-people/solution.py
class Solution:
    def distributeCandies(self, candies: int, num_people: int) -> list[int]:
        result = [class="syntax-number">0] * num_people; gift = class="syntax-number">1; person = class="syntax-number">0
        while candies:
            amount = min(gift, candies); result[person] += amount; candies -= amount
            gift, person = gift + class="syntax-number">1, (person + class="syntax-number">1) % num_people
        return result
TypeScript•distribute-candies-to-people/solution.ts
function distributeCandies(candies: number, numPeople: number): number[] {
  const result = new Array<number>(numPeople).fill(class="syntax-number">0); let gift = class="syntax-number">1; let person = class="syntax-number">0;
  while (candies > class="syntax-number">0) { const amount = Math.min(gift, candies); result[person] += amount; candies -= amount; gift += class="syntax-number">1; person = (person + class="syntax-number">1) % numPeople; }
  return result;
}