Ways to form a group from three groups with given constraints
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: 9
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
. 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
*
. - After calculation of all ways to fill up those two positions, we can simply multiply summation of
+
+ …
since that was the common part in both.
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; // Caculate 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; // Caculate 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 |
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; // Caculate 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 |
9
Time Complexity: O(K * K ), where K is the max of (x, y and z).
Recommended Posts:
- Ways of dividing a group into two halves such that two elements are in different groups
- Number of ways to arrange N items under given constraints
- Count ways to build street under given constraints
- Ways of selecting men and women from a group to make a team
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Count ways to form minimum product triplets
- Number of ways to form a heap with n distinct integers
- Number of ways to form an array with distinct adjacent elements
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Ways to form n/2 pairs such that difference of pairs is minimum
- Count of strings that can be formed using a, b and c under given constraints
- Count digit groupings of a number with given constraints
- Generate all permutations of a string that follow given constraints
- Find the longest path in a matrix with given constraints
- Maximize the happiness of the groups on the Trip
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



