Number of ways of distributing N identical objects in R distinct groups - GeeksforGeeks

07 May, 2021
Given two integers N and R, the task is to calculate the number of ways to distribute N identical objects into R distinct groups.
Examples: 
Input: N = 4, R = 2 Output: 5 No of objects in 1st group = 0, in second group = 4 No of objects in 1st group = 1, in second group = 3 No of objects in 1st group = 2, in second group = 2 No of objects in 1st group = 3, in second group = 1 No of objects in 1st group = 4, in second group = 0
Input: N = 4, R = 3 Output: 15 
Approach: Idea is to use Multinomial theorem. Let us suppose that x1 objects are placed in the first group, x2 objects are placed in the second group and xR objects are placed in the Rth group. It is given that, x1 + x2 + x3 +...+ xR = N The solution of this equation by multinomial theorem is given by N + R – 1CR – 1.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Function to return the// value of ncr effectivelyint ncr(int n, int r){     // Initialize the answer    int ans = 1;     for (int i = 1; i <= r; i += 1)    {        // Divide simultaneously by        // i to avoid overflow        ans *= (n - r + i);        ans /= i;    }    return ans;} // Function to return the number of// ways to distribute N identical// objects in R distinct objectsint NoOfDistributions(int N, int R){    return ncr(N + R - 1, R - 1);} // Driver codeint main(){    int N = 4, R = 3;     // Function call    cout << NoOfDistributions(N, R);     return 0;}
// Java implementation of the above approachimport java.util.*; class GFG {     // Function to return the    // value of ncr effectively    static int ncr(int n, int r)    {         // Initialize the answer        int ans = 1;         for (int i = 1; i <= r; i += 1)        {            // Divide simultaneously by            // i to avoid overflow            ans *= (n - r + i);            ans /= i;        }        return ans;    }     // Function to return the number of    // ways to distribute N identical    // objects in R distinct objects    static int NoOfDistributions(int N, int R)    {        return ncr(N + R - 1, R - 1);    }     // Driver code    public static void main(String[] args)    {        int N = 4, R = 3;               // Function call        System.out.println(NoOfDistributions(N, R));    }} // This code is contributed by Princi Singh
# Python3 implementation of the above approach # Function to return the# value of ncr effectively  def ncr(n, r):     # Initialize the answer    ans = 1     for i in range(1, r+1):         # Divide simultaneously by        # i to avoid overflow        ans *= (n - r + i)        ans //= i     return ans  # Function to return the number of# ways to distribute N identical# objects in R distinct objectsdef NoOfDistributions(N, R):     return ncr(N + R-1, R - 1)  # Driver codeN = 4R = 3 # Function callprint(NoOfDistributions(N, R)) # This code is contributed by mohit kumar 29
// C# implementation of the above approachusing System; class GFG {     // Function to return the    // value of ncr effectively    static int ncr(int n, int r)    {         // Initialize the answer        int ans = 1;         for (int i = 1; i <= r; i += 1)        {            // Divide simultaneously by            // i to avoid overflow            ans *= (n - r + i);            ans /= i;        }        return ans;    }     // Function to return the number of    // ways to distribute N identical    // objects in R distinct objects    static int NoOfDistributions(int N, int R)    {        return ncr(N + R - 1, R - 1);    }     // Driver code    static public void Main()    {        int N = 4, R = 3;               // Function call        Console.WriteLine(NoOfDistributions(N, R));    }} // This code is contributed by AnkitRai01
<script> // Javascript implementation of the above approach // Function to return the// value of ncr effectivelyfunction ncr(n, r){         // Initialize the answer    let ans = 1;     for(let i = 1; i <= r; i += 1)    {                 // Divide simultaneously by        // i to avoid overflow        ans *= (n - r + i);        ans = parseInt(ans / i);    }    return ans;} // Function to return the number of// ways to distribute N identical// objects in R distinct objectsfunction NoOfDistributions(N, R){    return ncr(N + R - 1, R - 1);} // Driver codelet N = 4, R = 3; // Function calldocument.write(NoOfDistributions(N, R));    // This code is contributed by subhammahato348     </script>
15
Time Complexity: O(R)
mohit kumar 29
ankthon
princi singh
anurag__tiwari_
subhammahato348
Numbers
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Sieve of Eratosthenes
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion
Operators in C / C++
Program for factorial of a number