- Related Questions & Answers
- Count the number of common divisors of the given strings in C++
- How to count the number of rows for a combination of categorical variables in R?
- Number of digits in the nth number made of given four digits in C++
- Find position of the given number among the numbers made of 4 and 7 in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Count of numbers which can be made power of 2 by given operation in C++
- Find char combination in array of strings JavaScript
- Generate Ranking with combination of strings in JavaScript
- Count Number of animals in a zoo from given number of head and legs in C++
- Maximum number of segments of lengths a, b and c in C++
- Count total divisors of A or B in a given range in C++
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Minimum number of given operations required to make two strings equal using C++.
- Count of sub-strings of length n possible from the given string in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given three numbers R, G and B and letters ‘R’, ‘G’ and ‘B’ only. The goal is to find the count of possible strings that can be made using at least R Rs, at least G Gs and at least B Bs in it. The numbers R, G and B have sum less than or equal to the length of strings possible.

**For Example**

R = 1, G = 1, B = 1 length=3

Count of number of strings (made of R, G and B) using given combination are − 6

The possible strings will be : “RGB”, “RBG”, “BRG”, “BGR”, “GRB”, “GBR”. That is permutations of RGB.

R = 2, G = 0, B = 2 length=4

Count of number of strings (made of R, G and B) using given combination are − 6

The possible strings will be : “RRBB”, “BBRR”, “RBRB”, “BRBR”, “RBBR”, “BRRB”.

**Approach used in the below program is as follows** −

In this approach we will first take letters for R, B and G number of times. Now check for remaining length − (R+G+B) letters make all permutations and combinations and add to count.

Take integer values R, G, B as input.

Take size as length of the strings to be made.

Function combination(int R, int G, int B, int size) takes all inputs and returns the count of number of strings (made of R, G and B) using a given combination.

Take the initial count as 0.

Take remaining count as temp=size − (R + G + B).

Take an array arr of length size+1 to take values of permutations.

Initially store factorials of i in arr[i]. Using for loop from i=0 to i=size. Set arr[i]=arr[i−1]*i.

For calculation of combinations traverse arr[] again using two for loops.

For i=0 to temp and j=0 to temp−i, compute temp_2 = temp − (i + j).

Take temp_3 = arr[i + R] * arr[j + B] * arr[temp_2 + G].

Now add arr[size] / temp_3 to count.

At the end of all for loops we will have counted as the required number of strings possible.

Return count as result.

#include<bits/stdc++.h> using namespace std; int combination(int R, int G, int B, int size){ int count = 0; int temp = size − (R + G + B); int arr[size+1]; arr[0] = 1; for (int i = 1; i <= size; i++){ arr[i] = arr[i − 1] * i; } for (int i = 0; i <= temp; i++){ for (int j = 0; j <= temp−i; j++){ int temp_2 = temp − (i + j); int temp_3 = arr[i + R] * arr[j + B] * arr[temp_2 + G]; count += arr[size] / temp_3; } } return count; } int main(){ int R = 2, G = 1, B = 1; int size = 4; cout<<"Count of number of strings (made of R, G and B) using given combination are: "<<combination(R, G, B, size); return 0; }

If we run the above code it will generate the following output −

Count of number of strings (made of R, G and B) using given combination are: 12

Advertisements