
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Code to Determine if an Image is B/W or Color
Suppose, we are given an image that contains n pixels. The pixels can be of the following color −
'C' (cyan)
'M' (magenta)
'Y' (yellow)
'W' (white)
'G' (grey)
'B' (black)
The color of the i-th pixel is given in the string 'pixels'. Given the string, we have to find out if the given photograph is colorful or black and white. If it is a color photograph it will contain at least one pixel of color 'C', 'M' and 'Y' and we will print 'Color'; otherwise, it will contain pixels of color 'W', 'G', 'B' only and we will print 'BW'.
So, if the input is like n = 10, pixels = "GBWYM", then the output will be Color.
Steps
To solve this, we will follow these steps −
for initialize i := 0, when i < n, update (increase i by 1), do: if pixels[i] is not equal to 'B' and pixels[i] is not equal to 'W' and pixels[i] is not equal to 'G', then: print("Color") return print("BW")
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, string pixels ) { for (int i = 0 ; i < n; i++) { if(pixels[i]!='B' && pixels[i]!='W' && pixels[i]!='G') { cout<<"Color"; return; } } cout<<"BW"; } int main() { int n = 10; string pixels = "GBWYM"; solve(n, pixels); return 0; }
Input
10, "GBWYM"
Output
Color
Advertisements