Mean of minimum of all possible K | Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code
function nCr ( n , r , f ) { if ( n < r ) { return 0 ; } return f [ n ] / ( f [ r ] * f [ n - r ] ) ; } function findMean ( N , X ) { let f = new Array ( N + 1 ) . fill ( 0 ) ; f [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) { f [ i ] = f [ i - 1 ] * i ; } let total = nCr ( N , X , f ) ; let count = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { count += nCr ( N - i , X - 1 , f ) * i ; } let E_X = count / total ; document . write ( parseFloat ( E_X ) . toFixed ( 9 ) ) ; return 0 ; } let N = 3 , X = 2 ; findMean ( N , X ) ;