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:3

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:6

**Approach:** Idea is to use Multinomial theorem. Let us suppose that **x _{1}** objects are placed in the first group,

**x**objects are placed in second group and

_{2}**x**objects are placed in the

_{R}**R**group. It is given that,

^{th}**x**for all

_{1}+ x_{2}+ x_{3}+…+ x_{R}= N**x**for 1 ≤ i ≤ R

_{i}≥ 1Now replace every

**x**with

_{i}**y**for all

_{i}+ 1**1 ≤ i ≤ R**. Now all the

**y**variaables are greater than or equal to zero.

The equation becomes,

**y**for all

_{1}+ y_{2}+ y_{3}+ … + y_{R}+ R = N**y**for 1 ≤ i ≤ R

_{i}≥ 0**y**

_{1}+ y_{2}+ y_{3}+ … + y_{R}= N – RIt now reduces to that standard multinomial equation whose solution is

**.**

^{(N – R) + R – 1}C_{R – 1}The solution of this equation is given by

**.**

^{N – 1}C_{R – 1}Below is the implementation of the above approach:

## CPP

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(R)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways of distributing N identical objects in R distinct groups
- Number of ways to arrange K different objects taking N objects at a time
- Distributing M items in a circle of size N starting from K-th position
- Probability of distributing M items among X bags such that first bag contains N items
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Count the number of ways to divide N in k groups incrementally
- Number of distinct ways to represent a number as sum of K unique primes
- Ways of dividing a group into two halves such that two elements are in different groups
- Check if given two straight lines are identical or not
- Minimum area of square holding two identical rectangles
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Count the number of ways to fill K boxes with N distinct items
- Count ways to split N! into two distinct co-prime factors
- Maximum number of objects that can be created as per given conditions
- Number of Groups of Sizes Two Or Three Divisible By 3
- Maximize number of groups formed with size not smaller than its largest element
- Maximize count of empty water bottles from N filled bottles
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.