Given 3 integers **R**, **G**, and **B** denoting the count of 3 colors Red, Green, and Blue respectively such that two different colors of the same quantity(say **X**) combine to form a third color of twice that quantity **2 * X**. The task is to check if it is possible to convert all the colors to a single color or not. If it is possible then print **“Yes”**. Otherwise, print “**No”**.

**Examples:**

Input:R = 1, G = 1, B = 1Output:YesExplanation:

Operation 1: Mix 1 unit of Red with 1 unit of Blue to obtain 2 units of Green.

Therefore, count of each colors are: R = 0, G = 3, B = 0

Hence, all the colors are converted to a single color.

Input:R = 1, G = 6, B = 3Output:YesExplanation:

Operation 1: Mix 1 unit of Red with 1 unit of Green to obtain 2 units of Blue.

Therefore, count of each colors are: R = 0, G = 5, B = 5

Operation 2: Mix 5 units of Green with 5 units of Blue to obtain 10 units of Red.

Therefore, count of each colors are: R = 10, G = 0, B = 0

Hence, all the colors are converted to a single color.

**Approach:** To change all the colors to the same color means the task is to reach the final state as **T = (0, 0, R + G + B) **or any of its other two permutations. Initially, the state is I = (R, G, B). After every operation, the values of the initial two colors reduce by one each and rise by two for the third color. This operation can be written as a permutation of **(-1, -1, +2) **based on chosen colors. Therefore, the following equation is formed:

N * op≡T mod 3where,Nis the number of operations to be performedopis the operationTis the final state

Using the above equation, observe that if the two values are equal after finding their modulus with 3, the given colors can be changed to one single color. Therefore, follow the steps below to solve the problem:

- Calculate modulo 3 of all given colors.
- Check for an equal pair.
- If found to be true, print “
**Yes”**. Otherwise, print “**No”**.

Below is the implementation of the above approach:

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`// Function to check whether it is` `// possible to do the operation or not` `bool` `isPossible(` `int` `r, ` `int` `b, ` `int` `g)`
`{` ` ` `// Calculate modulo 3`
` ` `// of all the colors`
` ` `r = r % 3;`
` ` `b = b % 3;`
` ` `g = g % 3;`
` ` `// Check for any equal pair`
` ` `if` `(r == b || b == g || g == r) {`
` ` `return` `true` `;`
` ` `}`
` ` `// Otherwise`
` ` `else` `{`
` ` `return` `false` `;`
` ` `}`
`}` `// Driver Code` `int` `main()`
`{` ` ` `// Given colors`
` ` `int` `R = 1, B = 3, G = 6;`
` ` `// Function Call`
` ` `if` `(isPossible(R, B, G)) {`
` ` `cout << ` `"Yes"` `<< endl;`
` ` `}`
` ` `else` `{`
` ` `cout << ` `"No"` `<< endl;`
` ` `}`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

`// Java program for ` `// the above approach` `import` `java.util.*;`
`class` `GFG{`
`// Function to check whether ` `// it is possible to do the ` `// operation or not` `static` `boolean` `isPossible(` `int` `r, `
` ` `int` `b, ` `int` `g)`
`{` ` ` `// Calculate modulo 3`
` ` `// of all the colors`
` ` `r = r % ` `3` `;`
` ` `b = b % ` `3` `;`
` ` `g = g % ` `3` `;`
` ` `// Check for any equal pair`
` ` `if` `(r == b || b == g || g == r) `
` ` `{`
` ` `return` `true` `;`
` ` `}`
` ` `// Otherwise`
` ` `else`
` ` `{`
` ` `return` `false` `;`
` ` `}`
`}` `// Driver Code` `public` `static` `void` `main(String[] args)`
`{` ` ` `// Given colors`
` ` `int` `R = ` `1` `, B = ` `3` `, G = ` `6` `;`
` ` `// Function Call`
` ` `if` `(isPossible(R, B, G)) `
` ` `{`
` ` `System.out.print(` `"Yes"` `+ ` `"\n"` `);`
` ` `}`
` ` `else`
` ` `{`
` ` `System.out.print(` `"No"` `+ ` `"\n"` `);`
` ` `}`
`}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

`# Python3 program for the above approach ` `# Function to check whether it is ` `# possible to do the operation or not` `def` `isPossible(r, b, g):`
` ` `# Calculate modulo 3`
` ` `# of all the colors`
` ` `r ` `=` `r ` `%` `3`
` ` `b ` `=` `b ` `%` `3`
` ` `g ` `=` `g ` `%` `3`
` ` `# Check for any equal pair`
` ` `if` `(r ` `=` `=` `b ` `or` `b ` `=` `=` `g ` `or` `g ` `=` `=` `r):`
` ` `return` `True`
` ` `# Otherwise`
` ` `else` `:`
` ` `return` `False`
`# Driver Code` `# Given colors` `R ` `=` `1`
`B ` `=` `3`
`G ` `=` `6`
`# Function call` `if` `(isPossible(R, B, G)):`
` ` `print` `(` `"Yes"` `)`
`else` `:`
` ` `print` `(` `"No"` `)`
`# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

`// C# program for ` `// the above approach` `using` `System;`
`class` `GFG{`
`// Function to check whether ` `// it is possible to do the ` `// operation or not` `static` `bool` `isPossible(` `int` `r, `
` ` `int` `b, ` `int` `g)`
`{` ` ` `// Calculate modulo 3`
` ` `// of all the colors`
` ` `r = r % 3;`
` ` `b = b % 3;`
` ` `g = g % 3;`
` ` `// Check for any equal pair`
` ` `if` `(r == b || b == g || g == r) `
` ` `{`
` ` `return` `true` `;`
` ` `}`
` ` `// Otherwise`
` ` `else`
` ` `{`
` ` `return` `false` `;`
` ` `}`
`}` `// Driver Code` `public` `static` `void` `Main(String[] args)`
`{` ` ` `// Given colors`
` ` `int` `R = 1, B = 3, G = 6;`
` ` `// Function Call`
` ` `if` `(isPossible(R, B, G)) `
` ` `{`
` ` `Console.Write(` `"Yes"` `+ ` `"\n"` `);`
` ` `}`
` ` `else`
` ` `{`
` ` `Console.Write(` `"No"` `+ ` `"\n"` `);`
` ` `}`
`}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(1)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Color N boxes using M colors such that K boxes have different color from the box on its left
- Find if it is possible to get a ratio from given ranges of costs and quantities
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Check if array can be converted into strictly decreasing sequence
- Check if A can be converted to B by reducing with a Prime number
- Minimum steps to color the tree with given colors
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
- Ways to color a 3*N board using 4 colors
- Ways to color a skewed tree such that parent and child have different colors
- Minimum number of colors required to color a Circular Array
- Convert the given RGB color code to Hex color code
- Check if given array can be made 0 with given operations performed any number of times
- Sort an Array alphabetically when each number is converted into words
- Check whether a number can be expressed as a product of single digit numbers
- Check if number can be made prime by deleting a single digit
- Program to Change RGB color model to HSV color model
- Color a grid such that all same color cells are connected either horizontally or vertically
- Check if all array elements can be removed by the given operations
- Check if sum Y can be obtained from the Array by the given operations

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.

**Article Tags :**