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:
- 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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int r, int b, int g)
{
r = r % 3;
b = b % 3;
g = g % 3;
if (r == b || b == g || g == r) {
return true ;
}
else {
return false ;
}
}
int main()
{
int R = 1, B = 3, G = 6;
if (isPossible(R, B, G)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPossible( int r,
int b, int g)
{
r = r % 3 ;
b = b % 3 ;
g = g % 3 ;
if (r == b || b == g || g == r)
{
return true ;
}
else
{
return false ;
}
}
public static void main(String[] args)
{
int R = 1 , B = 3 , G = 6 ;
if (isPossible(R, B, G))
{
System.out.print( "Yes" + "\n" );
}
else
{
System.out.print( "No" + "\n" );
}
}
}
|
Python3
def isPossible(r, b, g):
r = r % 3
b = b % 3
g = g % 3
if (r = = b or b = = g or g = = r):
return True
else :
return False
R = 1
B = 3
G = 6
if (isPossible(R, B, G)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPossible( int r,
int b, int g)
{
r = r % 3;
b = b % 3;
g = g % 3;
if (r == b || b == g || g == r)
{
return true ;
}
else
{
return false ;
}
}
public static void Main(String[] args)
{
int R = 1, B = 3, G = 6;
if (isPossible(R, B, G))
{
Console.Write( "Yes" + "\n" );
}
else
{
Console.Write( "No" + "\n" );
}
}
}
|
Javascript
<script>
function isPossible(r, b, g)
{
r = r % 3;
b = b % 3;
g = g % 3;
if (r == b || b == g || g == r) {
return true ;
}
else {
return false ;
}
}
var R = 1, B = 3, G = 6;
if (isPossible(R, B, G)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
29 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...