Find the resulting Colour Combination
Given a String of three Colours(G, B, Y) as input, the task is to print the resultant combined colour 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++
// C++ program to find the // resultant colour combination #include <iostream> using namespace std; // Function to return Colour Combination char Colour_Combination(string s) { char temp = s[0]; for (int i = 1; i < s.length(); i++) { if (temp != s[i]) { // Check for B * G = Y if ((temp == 'B' || temp == 'G') && (s[i] == 'G' || s[i] == 'B')) temp = 'Y'; // Check for B * Y = G else if ((temp == 'B' || temp == 'Y') && (s[i] == 'Y' || s[i] == 'B')) temp = 'G'; // Check for Y * G = B else temp = 'B'; } } return temp; } // Driver Code int main(int argc, char** argv) { string s = "GBYGB"; cout << Colour_Combination(s); } |
chevron_right
filter_none
Java
// Java program to find the // resultant colour combination class GfG { // Function to return Colour Combination static char Colour_Combination(String s) { char temp = s.charAt(0); for (int i = 1; i < s.length(); i++) { if (temp != s.charAt(i)) { // Check for B * G = Y if ((temp == 'B' || temp == 'G') && (s.charAt(i) == 'G' || s.charAt(i) == 'B')) temp = 'Y'; // Check for B * Y = G else if ((temp == 'B' || temp == 'Y') && (s.charAt(i) == 'Y' || s.charAt(i) == 'B')) temp = 'G'; // Check for Y * G = B else temp = 'B'; } } return temp; } // Driver code public static void main(String []args) { String s = "GBYGB"; System.out.println(Colour_Combination(s)); } } // This code is contributed by Rituraj Jain |
chevron_right
filter_none
Python3
# Python 3 program to find the # resultant colour combination # Function to return Colour Combination def Colour_Combination(s): temp = s[0] for i in range(1, len(s), 1): if (temp != s[i]): # Check for B * G = Y if ((temp == 'B' or temp == 'G') and (s[i] == 'G' or s[i] == 'B')): temp = 'Y' # Check for B * Y = G elif ((temp == 'B' or temp == 'Y') and (s[i] == 'Y' or s[i] == 'B')): temp = 'G' # Check for Y * G = B else: temp = 'B' return temp # Driver Code if __name__ == '__main__': s = "GBYGB" print(Colour_Combination(s)) # This code is contributed by # Surendra_Gangwar |
chevron_right
filter_none
C#
// C# program to find the // resultant colour combination using System; class GFG { // Function to return Colour Combination static char Colour_Combination(string s) { char temp = s[0]; for (int i = 1; i < s.Length; i++) { if (temp != s[i]) { // Check for B * G = Y if ((temp == 'B' || temp == 'G') && (s[i] == 'G' || s[i] == 'B')) temp = 'Y'; // Check for B * Y = G else if ((temp == 'B' || temp == 'Y') && (s[i] == 'Y' || s[i] == 'B')) temp = 'G'; // Check for Y * G = B else temp = 'B'; } } return temp; } // Driver Code static void Main() { string s = "GBYGB"; Console.WriteLine(Colour_Combination(s)); } } // This code is contributed by Ryuga |
chevron_right
filter_none
PHP
<?php // PHP program to find the // resultant colour combination // Function to return Colour Combination function Colour_Combination($s) { $temp = $s[0]; for ($i = 1; $i < strlen($s); $i++) { if ($temp != $s[$i]) { // Check for B * G = Y if (($temp == 'B' || $temp == 'G') && ($s[$i] == 'G' || $s[$i] == 'B')) $temp = 'Y'; // Check for B * Y = G else if (($temp == 'B' || $temp == 'Y') && ($s[$i] == 'Y' || $s[$i] == 'B')) $temp = 'G'; // Check for Y * G = B else $temp = 'B'; } } return $temp; } // Driver Code $s = "GBYGB"; echo Colour_Combination($s); // This code is contributed by ita_c ?> |
chevron_right
filter_none
Output:
B
Recommended Posts:
- Shortest possible combination of two strings
- Count number of strings (made of R, G and B) using given combination
- Python | Print all string combination from given numbers
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find the nth term of the given series
- Find Nth term of the series 1, 6, 18, 40, 75, ....
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Find substrings that contain all vowels
- Find elements of an Array which are Odd and Even using STL in C++
- Find sub-string with given power
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Program to find the initials of a name.
- How to find the sum of elements of an Array using STL in C++?
- Find the sum of the number of divisors
- Find Pth term of a GP if Mth and Nth terms are given
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.



