GENE01 - Editorial

PROBLEM LINK:

Practice
Div1
Div2
Div3

Setter: Srikkanth R
Tester: Aryan Choudhary
Editorialist: Ajit Sharma Kasturi

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

We are given two characters denoting the eye colors of two people in Chefland. The character R denotes Brown color, B denotes Blue color, and G denotes Green color. Green is the rarest of colors and Brown is the most common. The eye color of the child of two people is most likely to be the most common eye color between them. We need to determine the most likely eye color of the child of those two people given.

EXPLANATION:

  • Actually to the given problem statement, the order of most likely color of the child is R \gt B \gt G.

  • Therefore, if the two colors are R and B, we output R, if the two colors are B and G, we output B, if the two colors are G and R, we output R.

  • In case of both the given characters in the input are same, we obviously output that character itself.

TIME COMPLEXITY:

O(1) for each testcase.

SOLUTION:

Editorialist's solution
#include <iostream>
using namespace std;

int main() {
    
    char ch1, ch2;
    cin >> ch1 >> ch2;
    
    if ((ch1 == 'R' && ch2 == 'B') || (ch1 == 'B' && ch2 == 'R')) {
        cout << 'R' << endl;
    }
    else if ((ch1 == 'B' && ch2 == 'G') || (ch1 == 'G' && ch2 == 'B')) {
        cout << 'B' << endl;
    }
    else if ((ch1 == 'G' && ch2 == 'R') || (ch1 == 'R' && ch2 == 'G')) {
        cout << 'R' << endl;
    }
    else {
        cout << ch1 << endl;
    }
	    
	return 0;
}


Setter's solution
a = input()

if a == "R R" or a == "R B" or a == "R G" or a == "B R" or a == "G R":
    print("R")

elif a == "B B" or a == "B G" or a == "G B":
    print("B")

elif a == "G G":
    print("G")

Tester's solution
def main():
    s=input()
    for x in "RBG":
        if x in s:
            print(x)
            break

main()

Please comment below if you have any questions, alternate solutions, or suggestions. :slight_smile:

where does my code fails? thank you in advance
#include
using namespace std;

int main() {
char a,b;
cin>>a>>b;
if(a==‘R’||b==‘R’)
{
cout<<‘R’<<endl;
}
else if(a==‘G’||b==‘G’)
{
cout<<‘G’<<endl;
}
else
{
cout<<‘B’<<endl;
}
// your code goes here
return 0;
}

G B

Expected Output
B

Your Output
G

Where is error in my code, I used NODE JS, thanks in advance.

process.stdin.resume();
process.stdin.setEncoding(‘utf8’);

// your code goes here
process.stdin.on(‘data’, function(data){
let arr = data.split(’ ')
if(arr.includes(‘R’)){
console.log(‘R’)
}else if(arr.includes(‘B’)){
console.log(‘B’)
}else{
console.log(‘G’)
}
});

You should use else if(a == ‘B’ || b == 'B) as second statement that in order to maintain the likewise order, as the order is RBG, so first filter out the brown ones(R), then blue ones(B) and then green ones (G)