Update appNew update is available. Click here to update.

Distribute N candies among K people

Contributed by
Nishant Chitkara
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share
10 upvotes

Problem Statement

Suggest Edit

Sanyam has ‘N’ candies, he wants to distribute that into ‘K’ of his friends. He made his ‘K’ friends stand in line, in increasing order of his likeness. Not being so smart he gives 1 candy to the first friend, 2 to the second person, and so on till the kth person. In the next turn, the first person gets ‘K + 1’ candies, the second person gets ‘K + 2’ candies, and so on.

While distributing the candies, if at a turn, the number of candies to be given to a friend is less than the required candies, then that friend gets all the remaining candies and Sanyam stops the distribution.

Your task is to find the total number of candies every person has at the end.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 50
1 <= N <= 10^9
1 <= K <= 10^5

Time Limit: 1 sec
Sample Input 1:
2
7 4
4 1
Sample Output 1:
1 2 3 1
4
Explanation For Sample Input1:
Test case 1:
Sanyam has 7 candies and 4 friends.
In the first turn, the first friend gets 1 candy, the second friend gets 2 candies and the third friend gets 3 candies. Now he has used up 6 candies and 1 candy is left. As per the condition, the fourth friend has to be given 4 candies, but there is only 1 left, hence he takes one only. 

Test case 2:
As there is only one friend he will get the first candy, then the next two, and then the last one making the count 4.
Sample Input 2:
2
10 3
3 2
Sample Output 2:
5 2 3 
1 2
Reset Code
Full screen
Auto
copy-code
Console