Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Distribute Candies to People

XPChallenge Points: 10
levelLevel: Easy

You have a total number of candies and num_people people standing in a straight line.
Candies are given out in a repeating cycle, and each time the amount given increases:

  • First person receives 1 candy, the second gets 2 candies, and so on up to the last person.

  • After reaching the final person, the distribution loops back to the start, but the next gift continues increasing (next amount is previous + 1).

  • This pattern continues until the candies are not enough to give the full expected amount.
    The next person simply receives whatever candies are left.

Your task is to return an array representing how many candies each person ultimately receives.

Example 1:

Input: candies = 22, num_people = 5

Output: [7, 9, 3, 2, 1]

Explanation:

Candies are distributed in increasing order; once candies run low, the next person receives the remainder.

Example 2:

Input: candies = 15, num_people = 4

Output: [5, 7, 3, 0]

Explanation:

Distribution goes as: 1,2,3,4 → [1,2,3,4] 5,6,7,8 → candies finish before completing full cycle → remaining distributed → final result: [5,7,3,0]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result