Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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:
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

Algorithm:

  • Create a method named “fact” that takes a parameter n and returns the factorial of n.
    • Create a variable named “fact” and initialize it to 1.
    • Start a for loop and traverse through the range 2 to n (inclusive) and multiply each number with the “fact” variable.
    •  Return the value of “fact”.
  • Create another method named “ncr” that takes two parameters n and r, and returns the value of nCr.
    • Inside the “ncr” function, use the “fact” function to calculate the factorial of n, r, and n-r.
    • Calculate nCr using the formula n! / (r! * (n-r)!).
    • Return the value of nCr
  • Create another method “ways” that takes four parameters m, w, n, and k, and returns the total possible ways.
    • Create a variable named “ans” and initialize it to 0.
    • Start a while loop to iterate until m is greater than or equal to k.
      • Inside the while loop, add the value of nCr(m, k) multiplied by nCr(w, n-k) to the “ans” variable.
      •  Increment k by 1 in each iteration.                                                                                                                                    
  •  Return the value of “ans”.

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);
}


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


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


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


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


Javascript




<script>
// javascript implementation of the approach
 
// Returns factorial
// of the number
function fact(n)
{
    var fact = 1;
    for (i = 2; i <= n; i++)
        fact *= i;
    return fact;
}
 
// Function to calculate ncr
function ncr(n , r)
{
    var ncr = fact(n) / (fact(r) * fact(n - r));
    return parseInt(ncr);
}
 
// Function to calculate
// the total possible ways
function ways(m , w , n , k)
{
 
    var ans = 0;
    while (m >= k)
    {
        ans += ncr(m, k) * ncr(w, n - k);
        k += 1;
    }
 
    return parseInt(ans);
}
 
// Driver code
var m, w, n, k;
m = 7;
w = 6;
n = 5;
k = 3;
document.write( ways(m, w, n, k));
 
// This code is contributed by 29AjayKumar.
</script>


Output: 

756

 

Time Complexity: O(n)
Auxiliary Space: O(1)

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


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 17 Mar, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials