Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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


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


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


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to check whether it is
// possible to do the operation or not
function 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 || b == g || g == r) {
        return true;
    }
 
    // Otherwise
    else {
        return false;
    }
}
 
// Driver Code
// Given colors
var R = 1, B = 3, G = 6;
// Function Call
if (isPossible(R, B, G)) {
    document.write( "Yes");
}
else {
    document.write( "No");
}
 
 
</script>


Output: 

Yes

 

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



Last Updated : 29 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads