# Count of Ways to Choose N People With at Least X Men and Y Women from P Men and Q Women | Set 2

Given integers **N, P, Q, X, and Y, **the task is to find the number of ways to form a group of **N people** having **at least X men** and **Y women** from **P men and Q women, **where (X + Y â‰¤ N, X â‰¤ P and Y â‰¤ Q).

**Examples:**

Input:P = 4, Q = 2, N = 5, X = 3, Y = 1Output:6Explanation:Suppose given pool is {m1, m2, m3, m4} and {w1, w2}. Then possible combinations are:

m1 m2 m3 m4 w1

m1 m2 m3 m4 w2

m1 m2 m3 w1 w2

m1 m2 m4 w1 w2

m1 m3 m4 w1 w2

m2 m3 m4 w1 w2

Hence the count is 6.

Input:P = 5, Q = 2, N = 6, X = 4, Y = 1Output:7

**Naive Approach:** This problem is based on **combinatorics****, **and details of the Naive approach is already discussed in **Set-1** of this problem.

For some general value of P, Q, N, X and Y we can calculate the total possible ways using the following formula:

where

In this approach at every step we were calculating the value for each possible way.

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(1)

**Efficient Approach:** To solve this problem efficiently, we can use the **Pascal Triangle** property to calculate the , i.e.

1

1 1

1 2 1

1 3 3 1

.

.

.

which is nothing but

.

.

.

Follow the steps mentioned below:

- Use the
**pascal triangle**to precalculate the values of the combination. - Start iterating a loop from
**i = X**to**i = P**and do the following for each iteration. - Check if
**(N-i) â‰¥ Y**and**(N-i) â‰¤ Q**. - If the condition is satisfied then
**count**the possible ways for**i men**and**(N-i) women**, otherwise, skip the step. **Add**the count with the**total number**of ways.- Return the total count as your answer.

Below is the implementation of the approach:

## C++

`#include <bits/stdc++.h>` `using` `namespace` `std;` `long` `long` `int` `pascal[31][31];` `// Function to calculate the pascal triangle` `void` `pascalTriangle()` `{` ` ` `pascal[0][0] = 1;` ` ` `pascal[1][0] = 1;` ` ` `pascal[1][1] = 1;` ` ` `// Loop to calculate values of` ` ` `// pascal triangle` ` ` `for` `(` `int` `i = 2; i < 31; i++) {` ` ` `pascal[i][0] = 1;` ` ` `for` `(` `int` `j = 1; j < i; j++)` ` ` `pascal[i][j]` ` ` `= pascal[i - 1][j]` ` ` `+ pascal[i - 1][j - 1];` ` ` `pascal[i][i] = 1;` ` ` `}` `}` `// Function to calculate the number of ways` `long` `long` `int` `countWays(` `int` `n, ` `int` `p,` ` ` `int` `q, ` `int` `x,` ` ` `int` `y)` `{` ` ` `// Variable to store the answer` ` ` `long` `long` `int` `sum = 0;` ` ` `// Loop to calculate the number of ways` ` ` `for` `(` `long` `long` `int` `i = x; i <= p; i++) {` ` ` `if` `(n - i >= y && n - i <= q)` ` ` `sum += pascal[p][i]` ` ` `* pascal[q][n - i];` ` ` `}` ` ` `return` `sum;` `}` `// Driver code` `int` `main()` `{` ` ` `pascalTriangle();` ` ` `int` `P = 4, Q = 2, N = 5, X = 3, Y = 1;` ` ` `// Calculate possible ways for given` ` ` `// N, P, Q, X and Y` ` ` `cout << countWays(N, P, Q, X, Y)` ` ` `<< endl;` ` ` `return` `0;` `}` |

## Java

`// Java code for the above approach` `import` `java.io.*;` `class` `GFG {` ` ` `static` `long` `pascal[][] = ` `new` `long` `[` `31` `][` `31` `];` ` ` `// Function to calculate the pascal triangle` ` ` `static` `void` `pascalTriangle()` ` ` `{` ` ` `pascal[` `0` `][` `0` `] = ` `1` `;` ` ` `pascal[` `1` `][` `0` `] = ` `1` `;` ` ` `pascal[` `1` `][` `1` `] = ` `1` `;` ` ` `// Loop to calculate values of` ` ` `// pascal triangle` ` ` `for` `(` `int` `i = ` `2` `; i < ` `31` `; i++) {` ` ` `pascal[i][` `0` `] = ` `1` `;` ` ` `for` `(` `int` `j = ` `1` `; j < i; j++)` ` ` `pascal[i][j] = pascal[i - ` `1` `][j]` ` ` `+ pascal[i - ` `1` `][j - ` `1` `];` ` ` `pascal[i][i] = ` `1` `;` ` ` `}` ` ` `}` ` ` `// Function to calculate the number of ways` ` ` `static` `long` `countWays(` `int` `n, ` `int` `p, ` `int` `q, ` `int` `x, ` `int` `y)` ` ` `{` ` ` `// Variable to store the answer` ` ` `long` `sum = ` `0` `;` ` ` `// Loop to calculate the number of ways` ` ` `for` `(` `int` `i = x; i <= p; i++) {` ` ` `if` `(n - i >= y && n - i <= q)` ` ` `sum += pascal[p][i] * pascal[q][n - i];` ` ` `}` ` ` `return` `sum;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `pascalTriangle();` ` ` `int` `P = ` `4` `, Q = ` `2` `, N = ` `5` `, X = ` `3` `, Y = ` `1` `;` ` ` `// Calculate possible ways for given` ` ` `// N, P, Q, X and Y` ` ` `System.out.println(countWays(N, P, Q, X, Y));` ` ` `}` `}` `// This code is contributed by Potta Lokesh` |

## Python3

`pascal ` `=` `[[` `0` `for` `i ` `in` `range` `(` `31` `)] ` `for` `j ` `in` `range` `(` `31` `)]` `# Function to calculate the pascal triangle` `def` `pascalTriangle():` ` ` `pascal[` `0` `][` `0` `] ` `=` `1` `;` ` ` `pascal[` `1` `][` `0` `] ` `=` `1` `;` ` ` `pascal[` `1` `][` `1` `] ` `=` `1` `;` ` ` `# Loop to calculate values of` ` ` `# pascal triangle` ` ` `for` `i ` `in` `range` `(` `2` `, ` `31` `):` ` ` `pascal[i][` `0` `] ` `=` `1` `;` ` ` `for` `j ` `in` `range` `(i):` ` ` `pascal[i][j] ` `=` `pascal[i ` `-` `1` `][j] ` `+` `pascal[i ` `-` `1` `][j ` `-` `1` `];` ` ` `pascal[i][i] ` `=` `1` `;` `# Function to calculate the number of ways` `def` `countWays(n, p, q, x, y):` ` ` `# Variable to store the answer` ` ` `sum` `=` `0` `;` ` ` `# Loop to calculate the number of ways` ` ` `for` `i ` `in` `range` `(x, p ` `+` `1` `):` ` ` `if` `(n ` `-` `i >` `=` `y ` `and` `n ` `-` `i <` `=` `q):` ` ` `sum` `+` `=` `pascal[p][i] ` `*` `pascal[q][n ` `-` `i];` ` ` `return` `sum` `;` `# Driver code` `pascalTriangle();` `P ` `=` `4` `Q ` `=` `2` `N ` `=` `5` `X ` `=` `3` `Y ` `=` `1` `;` `# Calculate possible ways for given` `# N, P, Q, X and Y` `print` `(countWays(N, P, Q, X, Y))` `# This code is contributed by Saurabh Jaiswal` |

## C#

`// C# code for the above approach` `using` `System;` `class` `GFG{` `static` `long` `[,]pascal = ` `new` `long` `[31, 31];` `// Function to calculate the pascal triangle` `static` `void` `pascalTriangle()` `{` ` ` `pascal[0, 0] = 1;` ` ` `pascal[1, 0] = 1;` ` ` `pascal[1, 1] = 1;` ` ` `// Loop to calculate values of` ` ` `// pascal triangle` ` ` `for` `(` `int` `i = 2; i < 31; i++)` ` ` `{` ` ` `pascal[i, 0] = 1;` ` ` `for` `(` `int` `j = 1; j < i; j++)` ` ` `pascal[i, j] = pascal[i - 1, j] +` ` ` `pascal[i - 1, j - 1];` ` ` `pascal[i, i] = 1;` ` ` `}` `}` `// Function to calculate the number of ways` `static` `long` `countWays(` `int` `n, ` `int` `p, ` `int` `q, ` `int` `x, ` `int` `y)` `{` ` ` ` ` `// Variable to store the answer` ` ` `long` `sum = 0;` ` ` `// Loop to calculate the number of ways` ` ` `for` `(` `int` `i = x; i <= p; i++)` ` ` `{` ` ` `if` `(n - i >= y && n - i <= q)` ` ` `sum += pascal[p, i] * pascal[q, n - i];` ` ` `}` ` ` `return` `sum;` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` `pascalTriangle();` ` ` ` ` `int` `P = 4, Q = 2, N = 5, X = 3, Y = 1;` ` ` `// Calculate possible ways for given` ` ` `// N, P, Q, X and Y` ` ` `Console.WriteLine(countWays(N, P, Q, X, Y));` `}` `}` `// This code is contributed by shikhasingrajput` |

## Javascript

`<script>` `let pascal = ` `new` `Array(31).fill(0).map(() => ` `new` `Array(31).fill(0));` `// Function to calculate the pascal triangle` `function` `pascalTriangle() {` ` ` `pascal[0][0] = 1;` ` ` `pascal[1][0] = 1;` ` ` `pascal[1][1] = 1;` ` ` `// Loop to calculate values of` ` ` `// pascal triangle` ` ` `for` `(let i = 2; i < 31; i++) {` ` ` `pascal[i][0] = 1;` ` ` `for` `(let j = 1; j < i; j++)` ` ` `pascal[i][j]` ` ` `= pascal[i - 1][j]` ` ` `+ pascal[i - 1][j - 1];` ` ` `pascal[i][i] = 1;` ` ` `}` `}` `// Function to calculate the number of ways` `function` `countWays(n, p, q, x, y) {` ` ` `// Variable to store the answer` ` ` `let sum = 0;` ` ` `// Loop to calculate the number of ways` ` ` `for` `(let i = x; i <= p; i++) {` ` ` `if` `(n - i >= y && n - i <= q)` ` ` `sum += pascal[p][i]` ` ` `* pascal[q][n - i];` ` ` `}` ` ` `return` `sum;` `}` `// Driver code` `pascalTriangle();` `let P = 4, Q = 2, N = 5, X = 3, Y = 1;` `// Calculate possible ways for given` `// N, P, Q, X and Y` `document.write(countWays(N, P, Q, X, Y))` `// This code is contributed by gfgking.` `</script>` |

**Output**

6

**Time Complexity:** O(N)**Auxiliary Space:** O(N^{2})