Given four integers X, Y, M, and W. The task is to find the number of ways to choose X men and Y women from total M men and W women.
Examples:
Input: X = 1, Y = 2, M = 1, W = 3
Output: 3
Way 1: Choose the only man and 1st and 2nd women.
Way 2: Choose the only man and 2nd and 3rd women.
Way 3: Choose the only man and 1st and 3rd women.
Input: X = 4, Y = 3, M = 6, W = 5
Output: 150
Approach: The total number of ways of choosing X men from a total of M men is MCX and the total number of ways of choosing Y women from W women is WCY. Hence, the total number of combined ways will be MCX * WCY.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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;
}
int totalWays( int X, int Y, int M, int W)
{
return (ncr(M, X) * ncr(W, Y));
}
int main()
{
int X = 4, Y = 3, M = 6, W = 5;
cout << totalWays(X, Y, M, W);
return 0;
}
|
Java
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));
}
}
|
Python3
def ncr(n, r):
ans = 1
for i in range ( 1 ,r + 1 ):
ans * = (n - r + i)
ans / / = i
return ans
def totalWays(X, Y, M, W):
return (ncr(M, X) * ncr(W, Y))
X = 4
Y = 3
M = 6
W = 5
print (totalWays(X, Y, M, W))
|
C#
using System;
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));
}
static public void Main ()
{
int X = 4, Y = 3, M = 6, W = 5;
Console.WriteLine(totalWays(X, Y, M, W));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach:
Here, Another approach to solve this problem is to use the concept of permutations and combinations. We can choose X men from M men in MCX ways, and Y women from W women in WCY ways. To get the total number of ways of choosing X men and Y women, we need to multiply these two values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int factorial( int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int totalWays( int X, int Y, int M, int W)
{
int waysToChooseMen = 1;
for ( int i = M; i >= M - X + 1; i--)
{
waysToChooseMen *= i;
}
waysToChooseMen /= factorial(X);
int waysToChooseWomen = 1;
for ( int i = W; i >= W - Y + 1; i--)
{
waysToChooseWomen *= i;
}
waysToChooseWomen /= factorial(Y);
return waysToChooseMen * waysToChooseWomen;
}
int main ()
{
int X = 4, Y = 3, M = 6, W = 5;
cout << totalWays(X, Y, M, W);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int factorial( int n) {
if (n == 0 ) {
return 1 ;
} else {
return n * factorial(n - 1 );
}
}
static int totalWays( int X, int Y, int M, int W) {
int waysToChooseMen = 1 ;
for ( int i = M; i >= M - X + 1 ; i--) {
waysToChooseMen *= i;
}
waysToChooseMen /= factorial(X);
int waysToChooseWomen = 1 ;
for ( int i = W; i >= W - Y + 1 ; i--) {
waysToChooseWomen *= i;
}
waysToChooseWomen /= factorial(Y);
return waysToChooseMen * waysToChooseWomen;
}
public static void main(String[] args) {
int X = 4 , Y = 3 , M = 6 , W = 5 ;
System.out.println(totalWays(X, Y, M, W));
}
}
|
Python3
def factorial(n):
if n = = 0 :
return 1
else :
return n * factorial(n - 1 )
def totalWays(X, Y, M, W):
waysToChooseMen = 1
for i in range (M, M - X, - 1 ):
waysToChooseMen * = i
waysToChooseMen / / = factorial(X)
waysToChooseWomen = 1
for i in range (W, W - Y, - 1 ):
waysToChooseWomen * = i
waysToChooseWomen / / = factorial(Y)
return waysToChooseMen * waysToChooseWomen
if __name__ = = "__main__" :
X = 4
Y = 3
M = 6
W = 5
print (totalWays(X, Y, M, W))
|
C#
using System;
public class GFG
{
public static int Factorial( int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * Factorial(n - 1);
}
}
public static int TotalWays( int X, int Y, int M, int W)
{
int waysToChooseMen = 1;
for ( int i = M; i >= M - X + 1; i--)
{
waysToChooseMen *= i;
}
waysToChooseMen /= Factorial(X);
int waysToChooseWomen = 1;
for ( int i = W; i >= W - Y + 1; i--)
{
waysToChooseWomen *= i;
}
waysToChooseWomen /= Factorial(Y);
return waysToChooseMen * waysToChooseWomen;
}
public static void Main( string [] args)
{
int X = 4, Y = 3, M = 6, W = 5;
Console.WriteLine( "Total ways: " + TotalWays(X, Y, M, W));
}
}
|
Javascript
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
function totalWays(X, Y, M, W) {
let waysToChooseMen = 1;
for (let i = M; i >= M - X + 1; i--) {
waysToChooseMen *= i;
}
waysToChooseMen /= factorial(X);
let waysToChooseWomen = 1;
for (let i = W; i >= W - Y + 1; i--) {
waysToChooseWomen *= i;
}
waysToChooseWomen /= factorial(Y);
return waysToChooseMen * waysToChooseWomen;
}
function main() {
const X = 4, Y = 3, M = 6, W = 5;
console.log(totalWays(X, Y, M, W));
}
main();
|
Time Complexity:- O(X + Y)
Auxiliary Space:- O(1)