
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the resulting Colour Combination in C++
We have a string with three colors (G, B, Y). We have to find the resulting color based on these relations −
- B * G = Y
- Y * B = G
- G * Y = B
Suppose the string is “GBYGB” is B. If the string is “BYB”, then it will be Y.
The approach is simple; we will take the string. Compare each alphabet with adjacent characters, using the given condition, find the color.
Example
#include <iostream> using namespace std; char combination(string s) { char color = s[0]; for (int i = 1; i < s.length(); i++) { if (color != s[i]) { if ((color == 'B' || color == 'G') && (s[i] == 'G' || s[i] == 'B')) color = 'Y'; else if ((color == 'B' || color == 'Y') && (s[i] == 'Y' || s[i] == 'B')) color = 'G'; else color = 'B'; } } return color; } int main() { string color_str = "GBYBGY"; cout << "Color Combination Result: " << combination(color_str); }
Output
Color Combination Result: B
- Related Articles
- How to find the combination of matrix values in R?
- Find char combination in array of strings JavaScript
- How to get the colour name from colour code in R?
- How to find the unique combination of sum from the given number C#?
- Combination Sum in Python
- Substring combination in JavaScript
- I have 250 balls of two different colours. If red colour balls are 30 more than blue colour balls, then find the balls of each colour.
- Find the combination of columns for correlation coefficient greater than a certain value in R
- Colour Theory and Colour Psychology
- Permutation and Combination in Java
- Permutation and Combination in Python?
- Combination Sum IIII in C++
- Combination Sum IV in C++
- Combination Sum II in C++
- Iterator for Combination in C++

Advertisements