Count of All Possible Ways to Choose N People With at Least X Men and Y Women from P Men and Q Women
Last Updated :
03 Dec, 2021
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
Approach: This problem is based on combinatorics where we need to select at least X men out of P men available, and at least Y women out of Q women available, so that total people selected are N.Consider the example:
P = 4, Q = 2, N = 5, X = 3, Y = 1.
In this, the possible selections are:(4 men out of 4) * (1 women out of 2) + (3 men out of 4) * (2 woman out of 2)= 4C4 * 2C1 + 4C3 * 2C2
So for some general values of P, Q and N, the approach can be visualised as:
where
Follow the steps mentioned below to implement it:
- Start iterating a loop from i = X till i = P.
- Check if (N-i) satisfies the condition (N-i) ≥ Y and (N-i) ≤ Q. If the condition is satisfied then do as follows.
- At each iteration calculate the number of possible ways if we choose i men and (N-i) women.
- To get the number of possible ways for each iteration use the formula
- Add this value for each iteration with the total number of ways.
- Return the total value as your answer.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int fact( int f)
{
f++;
long long int ans = 1;
while (--f > 0)
ans = ans * f;
return ans;
}
long long int ncr( int n, int r)
{
return (fact(n) / (fact(r) * fact(n - r)));
}
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 += (ncr(p, i) * ncr(q, n - i));
}
return sum;
}
int main()
{
int P = 4, Q = 2, N = 5, X = 3, Y = 1;
cout << countWays(N, P, Q, X, Y) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static long fact( long f)
{
f++;
long ans = 1 ;
while (--f > 0 )
ans = ans * f;
return ans;
}
static long ncr( long n, long r)
{
return (fact(n) / (fact(r) * fact(n - r)));
}
static long countWays( int n, int p, int q,
int x, int y)
{
long sum = 0 ;
for ( long i = x; i <= p; i++) {
if (n - i >= y && n - i <= q)
sum += (( int )ncr(p, i) * ( int )ncr(q, n - i));
}
return sum;
}
public static void main(String args[])
{
int P = 4 , Q = 2 , N = 5 , X = 3 , Y = 1 ;
System.out.println(countWays(N, P, Q, X, Y));
}
}
|
Python3
def fact (f):
ans = 1
while (f):
ans = ans * f
f - = 1
return ans
def ncr (n, r):
return (fact(n) / / (fact(r) * fact(n - r)))
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 + = (ncr(p, i) * ncr(q, n - i))
return sum
P = 4
Q = 2
N = 5
X = 3
Y = 1
print (countWays(N, P, Q, X, Y))
|
C#
using System;
class GFG
{
static long fact( long f)
{
f++;
long ans = 1;
while (--f > 0)
ans = ans * f;
return ans;
}
static long ncr( long n, long r)
{
return (fact(n) / (fact(r) * fact(n - r)));
}
static long countWays( int n, int p, int q,
int x, int y)
{
long sum = 0;
for ( long i = x; i <= p; i++) {
if (n - i >= y && n - i <= q)
sum += (( int )ncr(p, i) * ( int )ncr(q, n - i));
}
return sum;
}
public static void Main()
{
int P = 4, Q = 2, N = 5, X = 3, Y = 1;
Console.Write(countWays(N, P, Q, X, Y));
}
}
|
Javascript
<script>
const fact = (f) => {
f++;
let ans = 1;
while (--f > 0)
ans = ans * f;
return ans;
}
const ncr = (n, r) => {
return (fact(n) / (fact(r) * fact(n - r)));
}
const countWays = (n, p, q, x, y) => {
let sum = 0;
for (let i = x; i <= p; i++) {
if (n - i >= y && n - i <= q)
sum += (ncr(p, i) * ncr(q, n - i));
}
return sum;
}
let P = 4, Q = 2, N = 5, X = 3, Y = 1;
document.write(countWays(N, P, Q, X, Y));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...