- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# 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

#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

- Related Questions & Answers
- Count the number of common divisors of the given strings in C++
- Number of digits in the nth number made of given four digits in C++
- How to count the number of rows for a combination of categorical variables in R?
- 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++
- 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++
- 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++
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- How to find the unique combination of sum from the given number C#?
- Minimum number of given operations required to make two strings equal using C++.
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++