Total ways of choosing X men and Y women from a total of M men and W women | JAVA implementation of the approach ; 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
import java . io . * ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int totalWays ( int X , int Y , int M , int W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } public static void main ( String [ ] args ) { int X = 4 , Y = 3 , M = 6 , W = 5 ; System . out . println ( totalWays ( X , Y , M , W ) ) ; } }