VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-ways-to-distribute-n-chocolates-among-k-persons/

⇱ Count Ways to Distribute N Chocolates among K persons - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count Ways to Distribute N Chocolates among K persons

Last Updated : 23 Jul, 2025

Given N Chocolates, distribute them among K Persons. We are also given an array of maxChocolates[] of size K. The Chocolates can be distributed in such a manner that the ith person has the capacity maxChocolates[i] (both inclusive). Find the total number of ways to distribute the chocolates such that no chocolates are left, and no person gets more chocolate than his capacity.

Examples:

Input: N = 3, K = 2, maxChocolates = [2, 3]
Output: 3
Explanation: There are 3 ways to distribute the chocolates:

  • Person1 gets 0 chocolate and Person2 gets 3 chocolates.
  • Person1 gets 1 chocolate and Person2 gets 2 chocolates.
  • Person1 gets 2 chocolates and Person2 gets 1 chocolate.

Input: N = 4, K = 2, maxChocolates = [1, 1]
Output: 0
Explanation: There is no way to distribute the 4 chocolates among these 2 persons.

Approach: The problem can be solved using the following appraoch:

The problem can be solved using Dynamic Programming. We can maintain a 2D array dp[][] such that dp[i][j] will store the number of ways to distribute i chocolates among j persons. Now, we iterate over all the persons, and for every person i, we explore all the possibilities of distributing the chocolates to the ith person.

dp[i][j] = number of ways to distribute i chocolates till the person at index j

Follow the steps mentioned below to implement the idea:

  • Declare a method countWays(i, remainingChocolates) which gives us the number of ways to distribute the remaining chocolates among the persons from index 0 to index i.
  • If i < 0, then we don't have any person left to give the chocolates to, so check if remainingChocolates is equal to zero as we need to distribute all the chocolates.
  • Check dp[i][remainingChocolates] to prevent unnecessary recursion calls.
  • Maintain count = 0 to store the number of ways
  • Explore all the possible cases by assigning the ith person 0,1,2 ... to minimum of the capacity of ith person and remainingChocolates and add their answers to count.
  • Return count to get the answer.

Below is the implementation of the above appraoch:


Output
3

Time Complexity: O(N * K * K), where N is the number of chocolates and K is the number of persons.
Auxiliary Space: O(N * K)

This iterative approach avoids recursion and calculates the result in a bottom-up manner using dynamic programming.

  • Create a 2D array dp of size (N+1) x (K+1) to store the number of ways to distribute i chocolates among j persons.
  • Initialize dp[i][0] to 1 for all i from 0 to N as there is only one way to distribute chocolates among 0 persons (i.e., no one).
  • Iterate over the persons from 1 to K and for each person j, iterate over the chocolates from 1 to N.
  • For each combination of person j and remaining chocolates i, calculate dp[i][j] by summing up the possibilities of assigning 0, 1, 2, ..., min(maxChocolates[j], i) chocolates to person j.
  • The answer will be stored in dp[N][K], representing the number of ways to distribute N chocolates among K persons.

Output
3

Time Complexity: O(N * K * maxChocolates_max), where maxChocolates_max is the maximum value in the maxChocolates array.
Auxiliary Space:O(N * K)

Comment