Open In App

Number of ways of distributing N identical objects in R distinct groups with no groups empty

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integer N and R, the task is to calculate the number of ways to distribute N identical objects into R distinct groups such that no groups are left empty.

Examples: 

Input: N = 4, R = 2 
Output:
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

Input: N = 5, R = 3 
Output:
 

Approach: Idea is to use Multinomial theorem. Let us suppose that x1 objects are placed in the first group, x2 objects are placed in second group and xR objects are placed in the Rth group. It is given that, 
x1 + x2 + x3 +…+ xR = N for all xi ? 1 for 1 ? i ? R 
Now replace every xi with yi + 1 for all 1 ? i ? R. Now all the y variables are greater than or equal to zero. 
The equation becomes, 
y1 + y2 + y3 + … + yR + R = N for all yi ? 0 for 1 ? i ? R 
y1 + y2 + y3 + … + yR = N – R 
It now reduces to that standard multinomial equation whose solution is (N – R) + R – 1CR – 1
The solution of this equation is given by N – 1CR – 1.

Below is the implementation of the above approach: 

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the
// value of ncr effectively
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
int NoOfDistributions(int N, int R)
{
    return ncr(N - 1, R - 1);
}
 
// Driver code
int main()
{
    int N = 4;
    int R = 3;
 
    cout << NoOfDistributions(N, R);
 
    return 0;
}


Java




// Java implementation of the above approach
import java.io.*;
 
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 - 1, R - 1);
    }
     
    // Driver code
    public static void main (String[] args)
    {
 
        int N = 4;
        int R = 3;
     
        System.out.println(NoOfDistributions(N, R));
    }
}
 
// This code is contributed by ajit


Python3




# 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 objects
def NoOfDistributions(N, R):
 
    return ncr(N - 1, R - 1)
 
# Driver code
 
N = 4
R = 3
 
print(NoOfDistributions(N, R))
 
# This code is contributed by mohit kumar 29


C#




// C# implementation of the above approach
using 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 - 1, R - 1);
    }
     
    // Driver code
    static public void Main ()
    {
        int N = 4;
        int R = 3;
     
        Console.WriteLine(NoOfDistributions(N, R));
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// Javascript implementation of the above approach
 
// Function to return the
// value of ncr effectively
function 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 objects
function NoOfDistributions(N, R)
{
    return ncr(N - 1, R - 1);
}
 
// Driver code
let N = 4;
let R = 3;
 
document.write(NoOfDistributions(N, R));
 
// This code is contributed by rishavmahato348
 
</script>


Output: 

3

 

Time Complexity: O(R)

Auxiliary Space: O(1)
 



Last Updated : 31 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads