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 = 1
Output: 6
Explanation: 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 = 1
Output: 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(N2)
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];
void pascalTriangle()
{
pascal[0][0] = 1;
pascal[1][0] = 1;
pascal[1][1] = 1;
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;
}
}
long long int countWays( int n, int p,
int q, int x,
int y)
{
long long int sum = 0;
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;
}
int main()
{
pascalTriangle();
int P = 4, Q = 2, N = 5, X = 3, Y = 1;
cout << countWays(N, P, Q, X, Y)
<< endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long pascal[][] = new long [ 31 ][ 31 ];
static void pascalTriangle()
{
pascal[ 0 ][ 0 ] = 1 ;
pascal[ 1 ][ 0 ] = 1 ;
pascal[ 1 ][ 1 ] = 1 ;
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 ;
}
}
static long countWays( int n, int p, int q, int x, int y)
{
long sum = 0 ;
for ( int i = x; i <= p; i++) {
if (n - i >= y && n - i <= q)
sum += pascal[p][i] * pascal[q][n - i];
}
return sum;
}
public static void main(String[] args)
{
pascalTriangle();
int P = 4 , Q = 2 , N = 5 , X = 3 , Y = 1 ;
System.out.println(countWays(N, P, Q, X, Y));
}
}
|
Python3
pascal = [[ 0 for i in range ( 31 )] for j in range ( 31 )]
def pascalTriangle():
pascal[ 0 ][ 0 ] = 1 ;
pascal[ 1 ][ 0 ] = 1 ;
pascal[ 1 ][ 1 ] = 1 ;
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 ;
def countWays(n, p, q, x, y):
sum = 0 ;
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 ;
pascalTriangle();
P = 4
Q = 2
N = 5
X = 3
Y = 1 ;
print (countWays(N, P, Q, X, Y))
|
C#
using System;
class GFG{
static long [,]pascal = new long [31, 31];
static void pascalTriangle()
{
pascal[0, 0] = 1;
pascal[1, 0] = 1;
pascal[1, 1] = 1;
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;
}
}
static long countWays( int n, int p, int q, int x, int y)
{
long sum = 0;
for ( int i = x; i <= p; i++)
{
if (n - i >= y && n - i <= q)
sum += pascal[p, i] * pascal[q, n - i];
}
return sum;
}
public static void Main(String[] args)
{
pascalTriangle();
int P = 4, Q = 2, N = 5, X = 3, Y = 1;
Console.WriteLine(countWays(N, P, Q, X, Y));
}
}
|
Javascript
<script>
let pascal = new Array(31).fill(0).map(() => new Array(31).fill(0));
function pascalTriangle() {
pascal[0][0] = 1;
pascal[1][0] = 1;
pascal[1][1] = 1;
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 countWays(n, p, q, x, y) {
let sum = 0;
for (let i = x; i <= p; i++) {
if (n - i >= y && n - i <= q)
sum += pascal[p][i]
* pascal[q][n - i];
}
return sum;
}
pascalTriangle();
let P = 4, Q = 2, N = 5, X = 3, Y = 1;
document.write(countWays(N, P, Q, X, Y))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N2)
Last Updated :
07 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...