Tuesday, December 5, 2017

Color Transformation - CTS

A trekker was once captured by a group of special creatures. The creatures resembled primary colors of light (red, blue and green). The creatures demanded the prisoner to write a program for the question which they are going to ask. The creatures refused to release the prisoner unless he answers the question correctly. The question is as follows.

These creatures had a special property. When two creatures of different colors meet with other, they get converted into a creature of the third color. The two creatures must be adjacent to each other to make the transformation take place. How many minimum number of creatures will be remaining after this transformation?

Input:
Single line input consists of an integer denoting the number of creatures. It is followed by the alphabets R, G and B denoting the color of the creature.

Output:
Print an integer denoting the minimum number of remaining creatures.

Sample:
1] Input: 5 R G B R B
    Output: 1
    Explanation: R and G combine to form B Now the array is B B R B. B and B can't combine thus R and B combine to form G. Now the array is modified as B B G. Now, B and G combine to form R. Finally, R combines with the remaining B to form G.

2] Input: 3 R R R
    Output: 3
    Explanation: Same colors (R R R) cannot combine, so they remain as such.

Solution:
#include <iostream>
#include <algorithm>
using namespace std;
int main(int x,char *s[])
{
    int i=1,n=atoi(s[1]);
    string v;
    while(n--)
      v+=s[++i];
    int a=count(v.begin(),v.end(),'R');
    int b=count(v.begin(),v.end(),'G');
    int c=count(v.begin(),v.end(),'B');
    if(a==0&&b==0||b==0&&c==0||c==0&&a==0)
        cout<<i-1;
    else if(a%2==0&&b%2==0&&c%2==0||a%2==1&&b%2==1&&c%2==1)
        cout<<"2";
    else
        cout<<"1";
    return 0;
}
Share: