Count ways to distribute m items among n people | function used to generate binomial coefficient time complexity O ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; m represents number of mangoes n represents number of people
function binomial_coefficient ( n , m ) { let res = 1 ; if ( m > n - m ) m = n - m ; for ( let i = 0 ; i < m ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function calculate_ways ( m , n ) { if ( m < n ) return 0 ; let ways = binomial_coefficient ( n + m - 1 , n - 1 ) ; return ways ; } let m = 7 , n = 5 ; let result = calculate_ways ( m , n ) ; document . write ( result ) ;