# Ways of selecting men and women from a group to make a team

Given four integers n, w, m and k where,

- m is the total number of men.
- w is the total number of women.
- n is the total number of people that need to be selected to form the team.
- k is the minimum number of men that have to be selected.

The task is to find the number of ways in which the team can be formed.

**Examples:**

Input:m = 2, w = 2, n = 3, k = 1

Output:4

There are 2 men, 2 women. We need to make a team of size 3 with at least one man and one woman. We can make the team in following ways.

m1 m2 w1

m1 w1 w2

m2 w1 w2

m1 m2 w2

Input:m = 7, w = 6, n = 5, k = 3

Output:756

Input:m = 5, w = 6, n = 6, k = 3

Output:281

**Approach:** Since, we have to take at least k men.

Totals ways = Ways when ‘k’ men are selected + Ways when ‘k+1’ men are selected + … + when ‘n’ men are selected

.

Taking the first example from above where out of 7 men and 6 women, total 5 people need to be selected with at least 3 men,

Number of ways = (7C3 x 6C2) + (7C4 x 6C1) + (7C5)

= 7 x 6 x 5 x 6 x 5 + (7C3 x 6C1) + (7C2)

= 525 + 7 x 6 x 5 x 6 + 7 x 6

= (525 + 210 + 21)

= 756

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns factorial ` `// of the number ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `fact *= i; ` ` ` `return` `fact; ` `} ` ` ` `// Function to calculate ncr ` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` `int` `ncr = fact(n) / (fact(r) * fact(n - r)); ` ` ` `return` `ncr; ` `} ` ` ` `// Function to calculate ` `// the total possible ways ` `int` `ways(` `int` `m, ` `int` `w, ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `int` `ans = 0; ` ` ` `while` `(m >= k) { ` ` ` `ans += ncr(m, k) * ncr(w, n - k); ` ` ` `k += 1; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `m, w, n, k; ` ` ` `m = 7; ` ` ` `w = 6; ` ` ` `n = 5; ` ` ` `k = 3; ` ` ` `cout << ways(m, w, n, k); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Returns factorial ` `// of the number ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `fact = ` `1` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `fact *= i; ` ` ` `return` `fact; ` `} ` ` ` `// Function to calculate ncr ` `static` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` `int` `ncr = fact(n) / (fact(r) * fact(n - r)); ` ` ` `return` `ncr; ` `} ` ` ` `// Function to calculate ` `// the total possible ways ` `static` `int` `ways(` `int` `m, ` `int` `w, ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `int` `ans = ` `0` `; ` ` ` `while` `(m >= k) { ` ` ` `ans += ncr(m, k) * ncr(w, n - k); ` ` ` `k += ` `1` `; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `m, w, n, k; ` ` ` `m = ` `7` `; ` ` ` `w = ` `6` `; ` ` ` `n = ` `5` `; ` ` ` `k = ` `3` `; ` ` ` `System.out.println( ways(m, w, n, k)); ` ` ` `} ` `} ` `// This Code is contributed ` `// by shs ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Returns factorial of the number ` `def` `fact(n): ` ` ` `fact ` `=` `1` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `fact ` `*` `=` `i ` ` ` `return` `fact ` ` ` `# Function to calculate ncr ` `def` `ncr(n, r): ` ` ` `ncr ` `=` `fact(n) ` `/` `/` `(fact(r) ` `*` `fact(n ` `-` `r)) ` ` ` `return` `ncr ` ` ` `# Function to calculate ` `# the total possible ways ` `def` `ways(m, w, n, k): ` ` ` `ans ` `=` `0` ` ` `while` `(m >` `=` `k): ` ` ` `ans ` `+` `=` `ncr(m, k) ` `*` `ncr(w, n ` `-` `k) ` ` ` `k ` `+` `=` `1` ` ` ` ` `return` `ans; ` ` ` `# Driver code ` `m ` `=` `7` `w ` `=` `6` `n ` `=` `5` `k ` `=` `3` `print` `(ways(m, w, n, k)) ` ` ` `# This code is contributed by sahishelangia ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` ` ` `class` `GFG { ` ` ` `// Returns factorial ` `// of the number ` `static` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `fact *= i; ` ` ` `return` `fact; ` `} ` ` ` `// Function to calculate ncr ` `static` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` `int` `ncr = fact(n) / (fact(r) * fact(n - r)); ` ` ` `return` `ncr; ` `} ` ` ` `// Function to calculate ` `// the total possible ways ` `static` `int` `ways(` `int` `m, ` `int` `w, ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `int` `ans = 0; ` ` ` `while` `(m >= k) { ` ` ` `ans += ncr(m, k) * ncr(w, n - k); ` ` ` `k += 1; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` ` ` `static` `void` `Main () { ` ` ` ` ` `int` `m, w, n, k; ` ` ` `m = 7; ` ` ` `w = 6; ` ` ` `n = 5; ` ` ` `k = 3; ` ` ` `System.Console.WriteLine( ways(m, w, n, k)); ` ` ` `} ` `} ` `// This Code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Returns factorial of the number ` `function` `fact(` `$n` `) ` `{ ` ` ` `$fact` `= 1; ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$fact` `*= ` `$i` `; ` ` ` `return` `$fact` `; ` `} ` ` ` `// Function to calculate ncr ` `function` `ncr(` `$n` `, ` `$r` `) ` `{ ` ` ` `$ncr` `= (int)(fact(` `$n` `) / (fact(` `$r` `) * ` ` ` `fact(` `$n` `- ` `$r` `))); ` ` ` `return` `$ncr` `; ` `} ` ` ` `// Function to calculate the total ` `// possible ways ` `function` `ways(` `$m` `, ` `$w` `, ` `$n` `, ` `$k` `) ` `{ ` ` ` `$ans` `= 0; ` ` ` `while` `(` `$m` `>= ` `$k` `) ` ` ` `{ ` ` ` `$ans` `+= ncr(` `$m` `, ` `$k` `) * ` ` ` `ncr(` `$w` `, ` `$n` `- ` `$k` `); ` ` ` `$k` `+= 1; ` ` ` `} ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$m` `= 7; ` `$w` `= 6; ` `$n` `= 5; ` `$k` `= 3; ` `echo` `ways(` `$m` `, ` `$w` `, ` `$n` `, ` `$k` `); ` ` ` `// This Code is contributed ` `// by Mukul Singh ` |

*chevron_right*

*filter_none*

**Output:**

756

**Further Optimization : **The above code can be optimized using faster algorithms for binomial coefficient computation.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Total ways of selecting a group of X men from N men with or without including a particular man
- Total ways of choosing X men and Y women from a total of M men and W women
- Ways to form a group from three groups with given constraints
- Ways of dividing a group into two halves such that two elements are in different groups
- Number of ways to make mobile lock pattern
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Minimum matches the team needs to win to qualify
- Minimize the cost of selecting two numbers whose product is X
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Group all co-prime numbers from 1 to N
- Find sum of N-th group of Natural Numbers
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Count of groups among N people having only one leader in each group
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Number Theory | Generators of finite cyclic group under addition
- Ways to represent a number as a sum of 1's and 2's
- Different ways to represent N as sum of K non-zero integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.