Distribute N candies among K people
Medium Accuracy: 53.56% Submissions: 3625 Points: 4

Given N candies and K people. In the first turn, the first person gets 1 candy, the second gets 2 candies, and so on till K people. In the next turn, the first person gets K+1 candies, the second person gets K+2 candies and so on. If the number of candies is less than the required number of candies at every turn, then the person receives the remaining number of candies. Find the total number of candies every person has at the end.


Example 1:

N = 7, K = 4
1 2 3 1
At the first turn, the fourth person
has to be given 4 candies, but there is
only 1 left, hence he takes only one. 
Example 2:
N = 10, K = 3
Output :
5 2 3
At the second turn first one receives
4 and then we have no more candies left. 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function distributeCandies() which takes 2 integers N and K as input and returns a list, where the ith integer denotes the number of candies the ith person gets.

Expected Time Complexity: O(logN+K)
Expected Auxiliary Space: O(K)

1 ≤ N ≤ 108
1 ≤ K ≤ 100

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Distribute N candies among K people

Output Window