You are given:
- an integer
candies, representing the total number of candies - an integer
num_people, representing how many people stand in a line
Candies must be distributed in this repeating pattern:
1st person gets 1 candy
2nd person gets 2 candies
3rd person gets 3 candies
… and so on
When reaching the last person, distribution continues again from the first person, increasing the next giveaway amount each time.
If candies run out, give the remaining amount to the current person.
Return an array showing how many candies each person ends up with.
Example:
Input: candies = 7, num_people = 4 Output: [1,2,3,1] Explanation: 1 → 1st person 2 → 2nd person 3 → 3rd person Remaining = 1 → 4th person
Problem Understanding
Distribution rules:
- Amount given increases by 1 each time
- Distribution cycles through people repeatedly
- Candy count decreases until zero
- Result array size equals num_people
Important behavior:
- We do not reset count when looping back
- Last allocation may be less than expected amount
Logic Explained in Simple English
Think about this process as:
- We maintain an array initialized with zeros, one slot per person.
- We keep a running number representing how many candies to give next (starting from 1).
- For each turn:
- Identify whose turn it is using modulo arithmetic
- Determine how many candies to give:
- If candies left ≥ required amount → give required amount
- Else → give remaining candies
- Subtract from candies
- Move to next person and increase giveaway count
We stop once candies reach zero.
Why this works:
- It simulates exactly what problem describes
- No need for mathematical shortcuts
- Complexity stays small because increments grow gradually
Step-by-Step Approach
- Create an array result[num_people] initialized to zeros
- Set
give = 1 - Set
index = 0 - While candies > 0:
- If candies >= give → result[index] += give
- Else → result[index] += candies
- Subtract given candies
- Increase give by 1
- Move index to next person using
(index + 1) % num_people
- Return result
Java Implementation
class Solution {
public int[] distributeCandies(int candies, int num_people) {
int[] result = new int[num_people];
int give = 1;
int index = 0;
while (candies > 0) {
int amount = Math.min(give, candies);
result[index] += amount;
candies -= amount;
give++;
index = (index + 1) % num_people;
}
return result;
}
}
Dry Run Example
Input:
candies = 10 num_people = 3
Initial:
result = [0,0,0] give = 1 index = 0
Turn 1
Give 1 to person 0
Remaining candies = 9
result = [1,0,0]
Turn 2
Give 2 to person 1
Remaining candies = 7
result = [1,2,0]
Turn 3
Give 3 to person 2
Remaining candies = 4
result = [1,2,3]
Turn 4
Give 4 to person 0
Remaining candies = 0
result = [5,2,3]
Stop.
Output:
[5,2,3]
Time and Space Complexity
Time Complexity
O(k)
Where k = number of distributions until candies run out
k grows roughly like √candies
Space Complexity
O(num_people)
Output array only
Common Mistakes and How to Avoid Them
Mistake 1: Resetting give to 1 after each full cycle
The give value must always increase, even after looping around.
Mistake 2: Not using Math.min for last allocation
Final person may receive less than expected.
Mistake 3: Incorrect index wrapping
Must wrap with modulo:
index = (index + 1) % num_people
Mistake 4: Overthinking with complex formulas
Simulation is simple and efficient enough.
Mistake 5: Assuming candies always distribute evenly
Many cases end mid-cycle.
