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.
The task is to find the total number of candies every person has at the end.
Examples:
Input: N = 7, K = 4
Output: 1 2 3 1
At the first turn, the fourth people has to be given 4 candies, but there is
only 1 left, hence he takes one only.
Input: N = 10, K = 3
Output: 5 2 3
At the second turn first one receives 4 and then we have no more candies left.
A naive approach is to iterate for every turn and distribute candies accordingly till candies are finished.
Time complexity: O(Number of distributions)
A better approach is to perform every turn in O(1) by calculating sum of natural numbers till the last term of series which will be (turns*k) and subtracting the sum of natural numbers till the last term of previous series which is (turns-1)*k. Keep doing this till the sum is less than N, once it exceeds then distribute candies in the given way till possible. We call a turn completed if every person gets the desired number of candies he is to get in a turn.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void candies( int n, int k)
{
int count = 0;
int ind = 1;
int arr[k];
memset (arr, 0, sizeof (arr));
while (n) {
int f1 = (ind - 1) * k;
int f2 = ind * k;
int sum1 = (f1 * (f1 + 1)) / 2;
int sum2 = (f2 * (f2 + 1)) / 2;
int res = sum2 - sum1;
if (res <= n) {
count++;
n -= res;
ind++;
}
else
{
int i = 0;
int term = ((ind - 1) * k) + 1;
while (n > 0) {
if (term <= n) {
arr[i++] = term;
n -= term;
term++;
}
else
{
arr[i++] = n;
n = 0;
}
}
}
}
for ( int i = 0; i < k; i++)
arr[i] += (count * (i + 1))
+ (k * (count * (count - 1)) / 2);
for ( int i = 0; i < k; i++)
cout << arr[i] << " " ;
}
int main()
{
int n = 10, k = 3;
candies(n, k);
return 0;
}
|
Java
class GFG {
static void candies( int n, int k){
int [] arr = new int [k];
int j = 0 ;
while (n> 0 ){
for ( int i = 0 ;i<k;i++){
j++;
if (n<= 0 ){
break ;
}
else {
if (j<n){
arr[i] = arr[i]+j;
}
else {
arr[i] = arr[i]+n;
}
n = n-j;
}
}
}
for ( int i:arr){
System.out.print(i+ " " );
}
}
public static void main(String[] args)
{
int n = 10 , k = 3 ;
candies(n, k);
}
}
|
Python3
import math as mt
def candies(n, k):
count = 0
ind = 1
arr = [ 0 for i in range (k)]
while n > 0 :
f1 = (ind - 1 ) * k
f2 = ind * k
sum1 = (f1 * (f1 + 1 )) / / 2
sum2 = (f2 * (f2 + 1 )) / / 2
res = sum2 - sum1
if (res < = n):
count + = 1
n - = res
ind + = 1
else :
i = 0
term = ((ind - 1 ) * k) + 1
while (n > 0 ):
if (term < = n):
arr[i] = term
i + = 1
n - = term
term + = 1
else :
arr[i] = n
i + = 1
n = 0
for i in range (k):
arr[i] + = ((count * (i + 1 )) +
(k * (count * (count - 1 )) / / 2 ))
for i in range (k):
print (arr[i], end = " " )
n, k = 10 , 3
candies(n, k)
|
C#
using System;
class GFG
{
static void candies( int n, int k)
{
int count = 0;
int ind = 1;
int []arr= new int [k];
for ( int i=0;i<k;i++)
arr[i]=0;
while (n>0) {
int f1 = (ind - 1) * k;
int f2 = ind * k;
int sum1 = (f1 * (f1 + 1)) / 2;
int sum2 = (f2 * (f2 + 1)) / 2;
int res = sum2 - sum1;
if (res <= n) {
count++;
n -= res;
ind++;
}
else
{
int i = 0;
int term = ((ind - 1) * k) + 1;
while (n > 0) {
if (term <= n) {
arr[i++] = term;
n -= term;
term++;
}
else
{
arr[i++] = n;
n = 0;
}
}
}
}
for ( int i = 0; i < k; i++)
arr[i] += (count * (i + 1))
+ (k * (count * (count - 1)) / 2);
for ( int i = 0; i < k; i++)
Console.Write( arr[i] + " " );
}
public static void Main()
{
int n = 10, k = 3;
candies(n, k);
}
}
|
PHP
<?php
function candies( $n , $k )
{
$count = 0;
$ind = 1;
$arr = array_fill (0, $k , 0) ;
while ( $n )
{
$f1 = ( $ind - 1) * $k ;
$f2 = $ind * $k ;
$sum1 = floor (( $f1 * ( $f1 + 1)) / 2);
$sum2 = floor (( $f2 * ( $f2 + 1)) / 2);
$res = $sum2 - $sum1 ;
if ( $res <= $n )
{
$count ++;
$n -= $res ;
$ind ++;
}
else
{
$i = 0;
$term = (( $ind - 1) * $k ) + 1;
while ( $n > 0)
{
if ( $term <= $n )
{
$arr [ $i ++] = $term ;
$n -= $term ;
$term ++;
}
else
{
$arr [ $i ++] = $n ;
$n = 0;
}
}
}
}
for ( $i = 0; $i < $k ; $i ++)
$arr [ $i ] += floor (( $count * ( $i + 1)) + ( $k *
( $count * ( $count - 1)) / 2));
for ( $i = 0; $i < $k ; $i ++)
echo $arr [ $i ], " " ;
}
$n = 10;
$k = 3;
candies( $n , $k );
?>
|
Javascript
<script>
function candies(n , k) {
var count = 0;
var ind = 1;
var arr = Array(k);
for (i = 0; i < k; i++)
arr[i] = 0;
while (n > 0) {
var f1 = (ind - 1) * k;
var f2 = ind * k;
var sum1 = (f1 * (f1 + 1)) / 2;
var sum2 = (f2 * (f2 + 1)) / 2;
var res = sum2 - sum1;
if (res <= n) {
count++;
n -= res;
ind++;
} else
{
var i = 0;
var term = ((ind - 1) * k) + 1;
while (n > 0) {
if (term <= n) {
arr[i++] = term;
n -= term;
term++;
} else
{
arr[i++] = n;
n = 0;
}
}
}
}
for (i = 0; i < k; i++)
arr[i] += (count * (i + 1)) +
(k * (count * (count - 1)) / 2);
for (i = 0; i < k; i++)
document.write(arr[i] + " " );
}
var n = 10, k = 3;
candies(n, k);
</script>
|
Time complexity: O(Number of turns + K)
Auxiliary Space: O(k)
An efficient approach is to find the largest number(say MAXI) whose sum upto natural numbers is less than N using Binary search. Since the last number will always be a multiple of K, we get the last number of complete turns. Subtract the summation till then from N. Distribute the remaining candies by traversing in the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void candies( int n, int k)
{
int count = 0;
int ind = 1;
int arr[k];
memset (arr, 0, sizeof (arr));
int low = 0, high = n;
while (low <= high) {
int mid = (low + high) >> 1;
int sum = (mid * (mid + 1)) >> 1;
if (sum <= n) {
count = mid / k;
low = mid + 1;
}
else {
high = mid - 1;
}
}
int last = (count * k);
n -= (last * (last + 1)) / 2;
int i = 0;
int term = (count * k) + 1;
while (n) {
if (term <= n) {
arr[i++] = term;
n -= term;
term++;
}
else {
arr[i] += n;
n = 0;
}
}
for ( int i = 0; i < k; i++)
arr[i] += (count * (i + 1))
+ (k * (count * (count - 1)) / 2);
for ( int i = 0; i < k; i++)
cout << arr[i] << " " ;
}
int main()
{
int n = 7, k = 4;
candies(n, k);
return 0;
}
|
Java
class GFG
{
static void candies( int n, int k)
{
int count = 0 ;
int ind = 1 ;
int []arr= new int [k];
for ( int i= 0 ;i<k;i++)
arr[i]= 0 ;
int low = 0 , high = n;
while (low <= high) {
int mid = (low + high) >> 1 ;
int sum = (mid * (mid + 1 )) >> 1 ;
if (sum <= n) {
count = mid / k;
low = mid + 1 ;
}
else {
high = mid - 1 ;
}
}
int last = (count * k);
n -= (last * (last + 1 )) / 2 ;
int j = 0 ;
int term = (count * k) + 1 ;
while (n > 0 ) {
if (term <= n) {
arr[j++] = term;
n -= term;
term++;
}
else {
arr[j] += n;
n = 0 ;
}
}
for ( int i = 0 ; i < k; i++)
arr[i] += (count * (i + 1 ))
+ (k * (count * (count - 1 )) / 2 );
for ( int i = 0 ; i < k; i++)
System.out.print( arr[i] + " " );
}
public static void main(String []args)
{
int n = 7 , k = 4 ;
candies(n, k);
}
}
|
Python3
def candies(n, k):
count = 0 ;
ind = 1 ;
arr = [ 0 ] * k;
low = 0 ;
high = n;
while (low < = high):
mid = (low + high) >> 1 ;
sum = (mid * (mid + 1 )) >> 1 ;
if ( sum < = n):
count = int (mid / k);
low = mid + 1 ;
else :
high = mid - 1 ;
last = (count * k);
n - = int ((last * (last + 1 )) / 2 );
i = 0 ;
term = (count * k) + 1 ;
while (n):
if (term < = n):
arr[i] = term;
i + = 1 ;
n - = term;
term + = 1 ;
else :
arr[i] + = n;
n = 0 ;
for i in range (k):
arr[i] + = ((count * (i + 1 )) +
int (k * (count * (count - 1 )) / 2 ));
for i in range (k):
print (arr[i], end = " " );
n = 7 ;
k = 4 ;
candies(n, k);
|
C#
using System;
class GFG
{
static void candies( int n, int k)
{
int count = 0;
int ind = 1;
int []arr= new int [k];
for ( int i=0;i<k;i++)
arr[i]=0;
int low = 0, high = n;
while (low <= high) {
int mid = (low + high) >> 1;
int sum = (mid * (mid + 1)) >> 1;
if (sum <= n) {
count = mid / k;
low = mid + 1;
}
else {
high = mid - 1;
}
}
int last = (count * k);
n -= (last * (last + 1)) / 2;
int j = 0;
int term = (count * k) + 1;
while (n > 0) {
if (term <= n) {
arr[j++] = term;
n -= term;
term++;
}
else {
arr[j] += n;
n = 0;
}
}
for ( int i = 0; i < k; i++)
arr[i] += (count * (i + 1))
+ (k * (count * (count - 1)) / 2);
for ( int i = 0; i < k; i++)
Console.Write( arr[i] + " " );
}
public static void Main()
{
int n = 7, k = 4;
candies(n, k);
}
}
|
PHP
<?php
function candies( $n , $k )
{
$count = 0;
$ind = 1;
$arr = array_fill (0, $k , 0);
$low = 0;
$high = $n ;
while ( $low <= $high )
{
$mid = ( $low + $high ) >> 1;
$sum = ( $mid * ( $mid + 1)) >> 1;
if ( $sum <= $n )
{
$count = (int)( $mid / $k );
$low = $mid + 1;
}
else
{
$high = $mid - 1;
}
}
$last = ( $count * $k );
$n -= (int)(( $last * ( $last + 1)) / 2);
$i = 0;
$term = ( $count * $k ) + 1;
while ( $n )
{
if ( $term <= $n )
{
$arr [ $i ++] = $term ;
$n -= $term ;
$term ++;
}
else
{
$arr [ $i ] += $n ;
$n = 0;
}
}
for ( $i = 0; $i < $k ; $i ++)
$arr [ $i ] += ( $count * ( $i + 1)) +
(int)( $k * ( $count * ( $count - 1)) / 2);
for ( $i = 0; $i < $k ; $i ++)
echo $arr [ $i ] . " " ;
}
$n = 7;
$k = 4;
candies( $n , $k );
?>
|
Javascript
<script>
function candies(n , k) {
var count = 0;
var ind = 1;
var arr = Array(k).fill(0);
for (i = 0; i < k; i++)
arr[i] = 0;
var low = 0, high = n;
while (low <= high) {
var mid = parseInt((low + high) /2);
var sum = parseInt((mid * (mid + 1)) / 2);
if (sum <= n) {
count = parseInt(mid / k);
low = mid + 1;
} else {
high = mid - 1;
}
}
var last = (count * k);
n -= (last * (last + 1)) / 2;
var j = 0;
var term = (count * k) + 1;
while (n > 0) {
if (term <= n) {
arr[j++] = term;
n -= term;
term++;
} else {
arr[j] += n;
n = 0;
}
}
for (i = 0; i < k; i++)
arr[i] += (count * (i + 1)) + (k * (count * (count - 1)) / 2);
for (i = 0; i < k; i++)
document.write(arr[i] + " " );
}
var n = 7, k = 4;
candies(n, k);
</script>
|
Time Complexity: O(log N + K)
Auxiliary Space: O(K) for given K
Distribute N candies among K people in c++:
Approach:
The distribute_candies function takes two integers as input: N, which represents the total number of candies, and K, which represents the number of people. It returns a vector of integers, where the i-th element represents the number of candies distributed to the i-th person.
The function initializes a vector result of K elements with zero candies. It then loops until all N candies have been distributed. In each iteration, it calculates the number of candies to give to the current person (candies_to_give) as the minimum of N and i+1. It then adds candies_to_give to the number of candies distributed to the i-th person in result, and subtracts candies_to_give from N. Finally, it increments i to move to the next person.
C++
#include <iostream>
#include <vector>
std::vector< int > distribute_candies( int N, int K) {
std::vector< int > result(K, 0);
int i = 0;
while (N > 0) {
int candies_to_give = std::min(N, i+1);
result[i % K] += candies_to_give;
N -= candies_to_give;
i += 1;
}
return result;
}
int main() {
int N = 10;
int K = 3;
std::vector< int > result = distribute_candies(N, K);
for ( int i = 0; i < K; i++) {
std::cout << result[i] << " " ;
}
return 0;
}
|
Java
import java.util.*;
public class DistributeCandies {
public static List<Integer> distributeCandies( int N,
int K)
{
List<Integer> result
= new ArrayList<>(Collections.nCopies(
K, 0 ));
int i = 0 ;
while (N > 0 ) {
int candiesToGive = Math.min(N, i + 1 );
result.set(
i % K,
result.get(i % K)
+ candiesToGive);
N -= candiesToGive;
i += 1 ;
}
return result;
}
public static void main(String[] args)
{
int N = 10 ;
int K = 3 ;
List<Integer> result = distributeCandies(N, K);
for ( int i = 0 ; i < K; i++) {
System.out.print(result.get(i) + " " );
}
}
}
|
Python3
def distribute_candies(N, K):
result = [ 0 ] * K
i = 0
while N > 0 :
candies_to_give = min (N, i + 1 )
result[i % K] + = candies_to_give
N - = candies_to_give
i + = 1
return result
if __name__ = = '__main__' :
N = 10
K = 3
result = distribute_candies(N, K)
for i in range (K):
print (result[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
public class Gfg {
public static List< int > distribute_candies( int N, int K) {
List< int > result = new List< int >( new int [K]);
int i = 0;
while (N > 0) {
int candies_to_give = Math.Min(N, i+1);
result[i % K] += candies_to_give;
N -= candies_to_give;
i += 1;
}
return result;
}
public static void Main() {
int N = 10;
int K = 3;
List< int > result = distribute_candies(N, K);
for ( int i = 0; i < K; i++) {
Console.Write(result[i] + " " );
}
}
}
|
Javascript
function distribute_candies(N, K) {
let result = Array(K).fill(0);
let i = 0;
while (N > 0) {
let candies_to_give = Math.min(N, i+1);
result[i % K] += candies_to_give;
N -= candies_to_give;
i += 1;
}
return result;
}
let N = 10;
let K = 3;
let result = distribute_candies(N, K); temp= "" ;
for (let i = 0; i < K; i++) {
temp = temp+result[i]+ " " ;
} console.log(temp);
|
The time complexity of this algorithm is O(N), because we need to distribute each of the N candies.
The auxiliary space of this algorithm is O(K), because we use a vector of K elements to store the candies distributed to each person.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Mar, 2023
Like Article
Save Article