# Count number of strings (made of R, G and B) using given combination in C++

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

## Input

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

## Output

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

## Explanation

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

## Input

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

## Output

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

## Explanation

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.

## Example

Live Demo

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

## Output

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