Problem
Given two integers ‘N’ and ‘M’, representing the total number of boys and girls respectively, you have to find the number of ways to form a team of size ‘K’, containing at least 4 boys and at least 1 girl.
Constraints:
4 <= N <= 25
1 <= M <= 25
5 <= K <= N + M.
Example -
Input: N = 5, M = 3, K = 6
Output: 18
Explanation:
The total size of the team is 6, which means we can either choose 4 boys and 2 girls or 5 boys and 1 girl.
- Number of ways to choose 4 boys out of 5 is 5C4 = 5, and the number of ways to choose 2 girls out of 3 is 3C2 = 3. Therefore, the number of ways to form a team of 4 boys and 2 girls is 5 * 3 = 15.
- Number of ways to choose 5 boys out of 5 is 5C5 = 1, and the number of ways to choose 1 girl out of 3 is 3C1 = 3. Therefore, the number of ways to form a team of 5 boys and 1 girl is 1 * 3 = 3.
Therefore, the total number of ways to form a team of size 6 is 15 + 3 = 18.
Input: N = 6, M = 4, K = 7
Output: 100
Also see, Euclid GCD Algorithm
Approach
Prerequisite - Combinations
For a fixed size K, let us say we can form a team (i, j) where ‘i’ denotes the number of boys, and ‘j’ denotes the number of girls in the team. The following are the constraints on (i, j):
- i + j = K
- 4 <= i <= N
- 1 <= j <= M
From the first equation, we get:
j = K - i
By substitutiog it in third equation, we get:
1 <= K - i <= M
Therefore, we need to form a team of size (i, K - i) if the following conditions hold:
- 4 <= i <= N
- 1<= K - i <= M
Now we can iterate over the ‘i’ to find the composition of the teams that we can form. We can calculate the number of teams to form a team of a particular composition (i, j) by -
NCi * MCj
To calculate the number of ways to form a team, we can add the answer over all the valid i and j.