Given three integers **N**, **M**, **X**. The task is to find the probability of distributing M items among X bags such that first bag contains N items

**Examples:**

Input :M = 7, X =3, N = 3

Output :0.2

The Number of ways to keep 7 items in 3 bags is .

The Number of ways to keep 4 items in 2 bags is . As the first bag contains 3 items.

The probability is /

Input :M = 9, X = 3, N = 4

Output :0.142857

**Approach :**

In general, the Number of ways to place N items in K bags is .

- The Number of ways to keep M items in X bags is .
- The Number of ways to keep (M-N) items in (X-1) bags is . As the first bag contains N items.
- The probability is /.

Below is the implementation of the above approach:

## C++

`// CPP program to find probability of ` `// first bag to contain N items such ` `// that M items are distributed among X bags ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find factorial of a number ` `int` `factorial(` `int` `n) ` `{ ` ` ` `if` `(n <= 1) ` ` ` `return` `1; ` ` ` `return` `n * factorial(n - 1); ` `} ` ` ` `// Function to find nCr ` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `return` `factorial(n) / (factorial(r) * factorial(n - r)); ` `} ` ` ` `// Function to find probability of ` `// first bag to contain N items such ` `// that M items are distributed among X bags ` `float` `Probability(` `int` `M, ` `int` `N, ` `int` `X) ` `{ ` ` ` `return` `(` `float` `)(nCr(M - N - 1, X - 2) / ` ` ` `(nCr(M - 1, X - 1) * 1.0)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `M = 9, X = 3, N = 4; ` ` ` ` ` `// Function call ` ` ` `cout << Probability(M, N, X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find probability of ` `// first bag to contain N items such ` `// that M items are distributed among X bags ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find factorial of a number ` ` ` `public` `static` `int` `factorial(` `int` `n) ` ` ` `{ ` ` ` `if` `(n <= ` `1` `) ` ` ` `return` `1` `; ` ` ` ` ` `return` `n * factorial(n - ` `1` `); ` ` ` `} ` ` ` ` ` `// Function to find nCr ` ` ` `public` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `return` `factorial(n) / (factorial(r) * factorial(n - r)); ` ` ` `} ` ` ` ` ` `// Function to find probability of ` ` ` `// first bag to contain N items such ` ` ` `// that M items are distributed among X bags ` ` ` `public` `static` `float` `Probability(` `int` `M, ` `int` `N, ` `int` `X) ` ` ` `{ ` ` ` `return` `(` `float` `) (nCr(M - N - ` `1` `, X - ` `2` `) / (nCr(M - ` `1` `, X - ` `1` `) * ` `1.0` `)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `M = ` `9` `, X = ` `3` `, N = ` `4` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(Probability(M, N, X)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find probability of ` `# first bag to contain N items such ` `# that M items are distributed among X bags ` ` ` `# Function to find factorial of a number ` `def` `factorial(n) : ` ` ` ` ` `if` `(n <` `=` `1` `) : ` ` ` `return` `1` `; ` ` ` ` ` `return` `n ` `*` `factorial(n ` `-` `1` `); ` ` ` `# Function to find nCr ` `def` `nCr(n, r) : ` ` ` ` ` `return` `(factorial(n) ` `/` `(factorial(r) ` `*` ` ` `factorial(n ` `-` `r))); ` ` ` `# Function to find probability of ` `# first bag to contain N items such ` `# that M items are distributed among X bags ` `def` `Probability(M, N, X) : ` ` ` ` ` `return` `float` `(nCr(M ` `-` `N ` `-` `1` `, X ` `-` `2` `) ` `/` ` ` `(nCr(M ` `-` `1` `, X ` `-` `1` `) ` `*` `1.0` `)); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `M ` `=` `9` `; X ` `=` `3` `; N ` `=` `4` `; ` ` ` ` ` `# Function call ` ` ` `print` `(Probability(M, N, X)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find probability of ` `// first bag to contain N items such ` `// that M items are distributed among X bags ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find factorial of a number ` ` ` `static` `int` `factorial(` `int` `n) ` ` ` `{ ` ` ` `if` `(n <= 1) ` ` ` `return` `1; ` ` ` ` ` `return` `n * factorial(n - 1); ` ` ` `} ` ` ` ` ` `// Function to find nCr ` ` ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `return` `factorial(n) / (factorial(r) * factorial(n - r)); ` ` ` `} ` ` ` ` ` `// Function to find probability of ` ` ` `// first bag to contain N items such ` ` ` `// that M items are distributed among X bags ` ` ` `static` `float` `Probability(` `int` `M, ` `int` `N, ` `int` `X) ` ` ` `{ ` ` ` `return` `(` `float` `) (nCr(M - N - 1, X - 2) / (nCr(M - 1, X - 1) * 1.0)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `M = 9, X = 3, N = 4; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine(Probability(M, N, X)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// mohitkumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

0.142857

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:

- Distributing M items in a circle of size N starting from K-th position
- Probability such that two subset contains same number of elements
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Count ways to distribute m items among n people
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Number of ways of distributing N identical objects in R distinct groups
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Maximum number of elements greater than X after equally distributing subset of array
- Program to find the profit or loss when CP of N items is equal to SP of M items
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Probability of cutting a rope into three pieces such that the sides form a triangle
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Convert a number of length N such that it contains any one digit at least 'K' times
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Find the smallest number X such that X! contains at least Y trailing zeros.

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.