Distribute N Candies Among K People

Explanation: Sanyam wishes to distribute 'N' candies among 'K' friends. The friends are arranged based on Sanyam's order of likeness. He initially distributes candies such that the 1st friend receives 1 candy, the 2nd friend receives 2 candies, up to the kth friend. In the subsequent rounds, the distribution is incremented by 'K' starting from the 1st friend. If during distribution, the remaining candies are fewer than what a friend is supposed to receive, that friend receives all remaining candies and the process stops.

Input: T test cases will follow. Each test case contains two integers, N and K where N is the number of candies and K is the number of friends.
Output: For each test case, output the number of candies each friend ends up with at the end of the distribution.

Example:

Input:
T = 1
N = 10
K = 3
Output:
[5, 2, 3]
Explanation:

In this example, the distribution is as follows:
- First, [1, 2, 3] are distributed.
- Second, [4, 5] are distributed and the process stops because there are 2 candies left, only enough for two friends.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 10^9
  • 1 <= K <= 10^5

Time Limit: 1 second

Be the first one to answer
Add answer anonymously...
GeeksForGeeks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter