Probability of distributing M items among X bags such that first bag contains N items | Java 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 ; Function to find nCr ; Function to find probability of first bag to contain N items such that M items are distributed among X bags ; Driver code ; Function call
class GFG { public static int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } public static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } 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 ) ) ; } public static void main ( String [ ] args ) { int M = 9 , X = 3 , N = 4 ; System . out . println ( Probability ( M , N , X ) ) ; } }