Find resulting Colour by Combination of given 3 colours in Array
Last Updated :
30 Nov, 2022
Given a String of three Colours(G, B, Y) as input, the task is to print the resultant combined color formed according to the rule given below:
// Rules for colour combination
- Blue(B) * Green(G) = Yellow(Y)
- Yellow(Y) * Blue(B) = Green(G)
- Green(G) * Yellow(Y) = Blue(B)
Examples:
Input: str = “GBYGB”
Output B
Input: str = “BYB”
Output Y
Approach: This problem can be solved as follows:
- Get the input string.
- Compare each alphabet with its adjacent characters.
- Use the above condition to determine the combination.
- print the output combination.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
char Colour_Combination(string s)
{
char temp = s[0];
for ( int i = 1; i < s.length(); i++) {
if (temp != s[i]) {
if ((temp == 'B' || temp == 'G' )
&& (s[i] == 'G' || s[i] == 'B' ))
temp = 'Y' ;
else if ((temp == 'B' || temp == 'Y' )
&& (s[i] == 'Y' || s[i] == 'B' ))
temp = 'G' ;
else
temp = 'B' ;
}
}
return temp;
}
int main( int argc, char ** argv)
{
string s = "GBYGB" ;
cout << Colour_Combination(s);
}
|
Java
class GfG
{
static char Colour_Combination(String s)
{
char temp = s.charAt( 0 );
for ( int i = 1 ; i < s.length(); i++)
{
if (temp != s.charAt(i))
{
if ((temp == 'B' || temp == 'G' )
&& (s.charAt(i) == 'G'
|| s.charAt(i) == 'B' ))
temp = 'Y' ;
else if ((temp == 'B' || temp == 'Y' )
&& (s.charAt(i) == 'Y'
|| s.charAt(i) == 'B' ))
temp = 'G' ;
else
temp = 'B' ;
}
}
return temp;
}
public static void main(String []args)
{
String s = "GBYGB" ;
System.out.println(Colour_Combination(s));
}
}
|
Python3
def Colour_Combination(s):
temp = s[ 0 ]
for i in range ( 1 , len (s), 1 ):
if (temp ! = s[i]):
if ((temp = = 'B' or temp = = 'G' ) and
(s[i] = = 'G' or s[i] = = 'B' )):
temp = 'Y'
elif ((temp = = 'B' or temp = = 'Y' ) and
(s[i] = = 'Y' or s[i] = = 'B' )):
temp = 'G'
else :
temp = 'B'
return temp
if __name__ = = '__main__' :
s = "GBYGB"
print (Colour_Combination(s))
|
C#
using System;
class GFG
{
static char Colour_Combination( string s)
{
char temp = s[0];
for ( int i = 1; i < s.Length; i++)
{
if (temp != s[i])
{
if ((temp == 'B' || temp == 'G' )
&& (s[i] == 'G' || s[i] == 'B' ))
temp = 'Y' ;
else if ((temp == 'B' || temp == 'Y' )
&& (s[i] == 'Y' || s[i] == 'B' ))
temp = 'G' ;
else
temp = 'B' ;
}
}
return temp;
}
static void Main()
{
string s = "GBYGB" ;
Console.WriteLine(Colour_Combination(s));
}
}
|
PHP
<?php
function Colour_Combination( $s )
{
$temp = $s [0];
for ( $i = 1; $i < strlen ( $s ); $i ++)
{
if ( $temp != $s [ $i ])
{
if (( $temp == 'B' || $temp == 'G' ) &&
( $s [ $i ] == 'G' || $s [ $i ] == 'B' ))
$temp = 'Y' ;
else if (( $temp == 'B' || $temp == 'Y' ) &&
( $s [ $i ] == 'Y' || $s [ $i ] == 'B' ))
$temp = 'G' ;
else
$temp = 'B' ;
}
}
return $temp ;
}
$s = "GBYGB" ;
echo Colour_Combination( $s );
?>
|
Javascript
<script>
function Colour_Combination(s)
{
let temp = s[0];
for (let i = 1; i < s.length; i++)
{
if (temp != s[i])
{
if ((temp == 'B' || temp == 'G' ) &&
(s[i] == 'G' || s[i] == 'B' ))
temp = 'Y' ;
else if ((temp == 'B' || temp == 'Y' ) &&
(s[i] == 'Y' || s[i] == 'B' ))
temp = 'G' ;
else
temp = 'B' ;
}
}
return temp;
}
let s = "GBYGB" ;
document.write(Colour_Combination(s));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...