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++

`// 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

`// 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

`# 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#

`// 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
- Sort an Array alphabetically when each number is converted into words
- 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
- 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
- Maximum distinct lines passing through a single point
- Integers from the range that are composed of a single distinct digit
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Program to Change RGB color model to HSV color model

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.