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
< ? php function binomial_coefficient ( $ n , $ m ) { $ res = 1 ; if ( $ m > $ n - $ m ) $ m = $ n - $ m ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function calculate_ways ( $ m , $ n ) { if ( $ m < $ n ) return 0 ; $ ways = binomial_coefficient ( $ n + $ m - 1 , $ n - 1 ) ; return $ ways ; } $ m = 7 ; $ n = 5 ; $ result = calculate_ways ( $ m , $ n ) ; echo $ result ; ? >