Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations

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 = 1
Output: Yes
Explanation:
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 = 3
Output: Yes
Explanation:
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 3 where, 
N is the number of operations to be performed
op is the operation
T is 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:

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.