Total ways of choosing X men and Y women from a total of M men and W women | Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ; Driver Code
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 totalWays ( X , Y , M , W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } let X = 4 , Y = 3 , M = 6 , W = 5 ; document . write ( totalWays ( X , Y , M , W ) ) ;