Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ways to form a group from three groups with given constraints

  • Last Updated : 28 May, 2021

Given three numbers(x, y and z) which denote the number of people in the first group, the second group, and third group. We can form groups by selecting people from the first group, the second group and third group such that the following conditions are not void. 
 

  • A minimum of one people has to be selected from every group.
  • The number of people selected from the first group has to be at least one more than the number of people selected from the third group.

The task is to find the number of ways of forming distinct groups. 
Examples: 
 

Input: x = 3, y = 2, z = 1 
Output:
Lets say x has people (a, b, c) 
Y has people (d, e) 
Z has people (f) 
Then the 9 ways are {a, b, d, f}, {a, b, e, f}, {a, c, d, f}, {a, c, e, f}, 
{b, c, d, f}, {b, c, e, f}, {a, b, c, d, f}, {a, b, c, e, f} and 
{a, b, c, d, e, f}
Input: x = 4, y = 2, z = 1 
Output: 27 
 

 

The problem can be solved using combinatorics. There are three positions(in terms of people from different groups) which needs to be filled. The first has to be filled with a number with one or more greater than the second position. The third can be filled with any number. We know if we need to fill k positions with N people, then the number of ways of doing it is {N \choose K}   . Hence the following steps can be followed to solve the above problem. 
 

  • The second position can be filled with i = 1 to i = y people.
  • The first position can be filled with j = i+1 to j = x people.
  • The third position can be filled with any number of k = 1 to k = z people.
  • Hence the common thing is filling the third position with k people. Hence we can take that portion as common.
  • Run two loops( i and j) for filling up the second position and first position respectively.
  • The number of ways of filling up the positions are {y \choose i}   {x \choose j}   .
  • After calculation of all ways to fill up those two positions, we can simply multiply summation of {z \choose 1}   {z \choose 2}   + … {z \choose z}   since that was the common part in both.

{N \choose K}   can be pre-computed using Dynamic Programming to reduce time complexity. The method is discussed here
Below is the implementation of the above approach. 
 

C++




// C++ program to find the number of
// ways to form the group of peopls
#include <bits/stdc++.h>
using namespace std;
 
int C[1000][1000];
 
// Function to pre-compute the
// Combination using DP
void binomialCoeff(int n)
{
    int i, j;
 
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++) {
        for (j = 0; j <= i; j++) {
 
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
 
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
 
    // return C[n][k];
}
 
// Function to find the number of ways
int numberOfWays(int x, int y, int z)
{
    // Function to pre-compute
    binomialCoeff(max(x, max(y, z)));
 
    // Sum the Zci
    int sum = 0;
    for (int i = 1; i <= z; i++) {
        sum = (sum + C[z][i]);
    }
 
    // Iterate for second position
    int sum1 = 0;
    for (int i = 1; i <= y; i++) {
 
        // Iterate for first position
        for (int j = i + 1; j <= x; j++) {
            sum1 = (sum1 + (C[y][i] * C[x][j]));
        }
    }
 
    // Multiply the common Combination value
    sum1 = (sum * sum1);
 
    return sum1;
}
 
// Driver Code
int main()
{
    int x = 3;
    int y = 2;
    int z = 1;
 
    cout << numberOfWays(x, y, z);
 
    return 0;
}

Java




// Java program to find the number of
// ways to form the group of peopls
class GFG
{
     
static int C[][] = new int [1000][1000];
 
// Function to pre-compute the
// Combination using DP
static void binomialCoeff(int n)
{
    int i, j;
 
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= i; j++)
        {
 
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
 
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
 
    // return C[n][k];
}
 
// Function to find the number of ways
static int numberOfWays(int x, int y, int z)
{
    // Function to pre-compute
    binomialCoeff(Math.max(x, Math.max(y, z)));
 
    // Sum the Zci
    int sum = 0;
    for (int i = 1; i <= z; i++)
    {
        sum = (sum + C[z][i]);
    }
 
    // Iterate for second position
    int sum1 = 0;
    for (int i = 1; i <= y; i++)
    {
 
        // Iterate for first position
        for (int j = i + 1; j <= x; j++)
        {
            sum1 = (sum1 + (C[y][i] * C[x][j]));
        }
    }
 
    // Multiply the common Combination value
    sum1 = (sum * sum1);
 
    return sum1;
}
 
// Driver Code
public static void main(String args[])
{
    int x = 3;
    int y = 2;
    int z = 1;
 
    System.out.println(numberOfWays(x, y, z));
}
}
 
// This code is contributed by Arnab Kundu

Python3




# Python3 program to find the number of
# ways to form the group of peopls
C = [[0 for i in range(1000)]
        for i in range(1000)]
 
# Function to pre-compute the
# Combination using DP
def binomialCoeff(n):
    i, j = 0, 0
 
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(n + 1):
        for j in range(i + 1):
 
            # Base Cases
            if (j == 0 or j == i):
                C[i][j] = 1
 
            # Calculate value using previously
            # stored values
            else:
                C[i][j] = C[i - 1][j - 1] + \
                          C[i - 1][j]
 
    # return C[n][k]
 
# Function to find the number of ways
def numberOfWays(x, y, z):
     
    # Function to pre-compute
    binomialCoeff(max(x, max(y, z)))
 
    # Sum the Zci
    sum = 0
    for i in range(1, z + 1):
        sum = (sum + C[z][i])
 
    # Iterate for second position
    sum1 = 0
    for i in range(1, y + 1):
 
        # Iterate for first position
        for j in range(i + 1, x + 1):
            sum1 = (sum1 + (C[y][i] * C[x][j]))
 
    # Multiply the common Combination value
    sum1 = (sum * sum1)
 
    return sum1
 
# Driver Code
x = 3
y = 2
z = 1
 
print(numberOfWays(x, y, z))
 
# This code is contributed by Mohit Kumar

C#




// C# program to find the number of
// ways to form the group of peopls
using System;
     
class GFG
{
     
static int [,]C = new int [1000,1000];
 
// Function to pre-compute the
// Combination using DP
static void binomialCoeff(int n)
{
    int i, j;
 
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= i; j++)
        {
 
            // Base Cases
            if (j == 0 || j == i)
                C[i,j] = 1;
 
            // Calculate value using previously
            // stored values
            else
                C[i,j] = C[i - 1,j - 1] + C[i - 1,j];
        }
    }
 
    // return C[n,k];
}
 
// Function to find the number of ways
static int numberOfWays(int x, int y, int z)
{
    // Function to pre-compute
    binomialCoeff(Math.Max(x, Math.Max(y, z)));
 
    // Sum the Zci
    int sum = 0;
    for (int i = 1; i <= z; i++)
    {
        sum = (sum + C[z,i]);
    }
 
    // Iterate for second position
    int sum1 = 0;
    for (int i = 1; i <= y; i++)
    {
 
        // Iterate for first position
        for (int j = i + 1; j <= x; j++)
        {
            sum1 = (sum1 + (C[y,i] * C[x,j]));
        }
    }
 
    // Multiply the common Combination value
    sum1 = (sum * sum1);
 
    return sum1;
}
 
// Driver Code
public static void Main(String []args)
{
    int x = 3;
    int y = 2;
    int z = 1;
 
    Console.WriteLine(numberOfWays(x, y, z));
}
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
// javascript program to find the number of
// ways to form the group of peopls    
var C = Array(1000).fill().map(()=>Array(1000).fill(0));
 
    // Function to pre-compute the
    // Combination using DP
    function binomialCoeff(n) {
        var i, j;
 
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (i = 0; i <= n; i++) {
            for (j = 0; j <= i; j++) {
 
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
 
                // Calculate value using previously
                // stored values
                else
                    C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
            }
        }
 
        // return C[n][k];
    }
 
    // Function to find the number of ways
    function numberOfWays(x , y , z) {
        // Function to pre-compute
        binomialCoeff(Math.max(x, Math.max(y, z)));
 
        // Sum the Zci
        var sum = 0;
        for (i = 1; i <= z; i++) {
            sum = (sum + C[z][i]);
        }
 
        // Iterate for second position
        var sum1 = 0;
        for (i = 1; i <= y; i++) {
 
            // Iterate for first position
            for (j = i + 1; j <= x; j++) {
                sum1 = (sum1 + (C[y][i] * C[x][j]));
            }
        }
 
        // Multiply the common Combination value
        sum1 = (sum * sum1);
 
        return sum1;
    }
 
    // Driver Code
     
        var x = 3;
        var y = 2;
        var z = 1;
 
        document.write(numberOfWays(x, y, z));
 
// This code contributed by aashish1995
</script>
Output: 
9

 

Time Complexity: O(K * K ), where K is the max of (x, y and z).
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!