Number of ways of distributing N identical objects in R distinct groups | Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code ; Function call
function ncr ( n , r ) { let ans = 1 ; for ( let i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans = parseInt ( ans / i ) ; } return ans ; } function NoOfDistributions ( N , R ) { return ncr ( N + R - 1 , R - 1 ) ; } let N = 4 , R = 3 ; document . write ( NoOfDistributions ( N , R ) ) ;