Roy and LEDs

It's Diwali time and there are LED series lights everywhere. Little Roy got curious about how LED lights work.

He noticed that in one single LED Bulb there are 3 LED lights, namely Red, Green, and Blue.

The state of the bulb at any moment is the sum of Red, Green, and Blue LED lights.

Roy took out all the LEDs and found that Red LED stays ON for R seconds, Green LED stays ON for G seconds and Blue LED stays ON for B seconds. Similarly, they stay OFF for the same respective R, G, B number of seconds. (Initially, all the LEDs are OFF. See Sample Test Case Explanation for better understanding)

Roy has one query for you, given the total number of seconds T, find the number of seconds Red, Green, Blue, Yellow, Cyan, Magenta, White, Black(no light) lights are visible.

Example:

Input:  n = 12, r = 2, g = 3, b = 5
Output: 1 1 1 3 2 2 0 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void royAndLeds(int nint rint gint b)
{
    vector<intredgreenblue;
    int i = 0cntr = 0cntg = 0cntb = 0;
    int flag = 0;
    while (i < n)
    {
        red.push_back(flag);
        cntr++;
        i++;
        if (cntr == r)
        {
            flag = !flag;
            cntr = 0;
        }
    }

    flag = 0i = 0;
    while (i < n)
    {
        green.push_back(flag);
        cntg++;
        i++;
        if (cntg == g)
        {
            flag = !flag;
            cntg = 0;
        }
    }

    flag = 0i = 0;
    while (i < n)
    {
        blue.push_back(flag);
        cntb++;
        i++;
        if (cntb == b)
        {
            flag = !flag;
            cntb = 0;
        }
    }
    int Red = 0Green = 0Blue = 0;
    int Yellow = 0Cyan = 0Magenta = 0;
    int White = 0Black = 0;
    for (int i = 0i < ni++)
    {
        if (red[i] == 0 && green[i] == 0 && blue[i] == 0)
            Black++;
        else if (red[i] == 1 && green[i] == 1 && blue[i] == 1)
            White++;
        else if (red[i] == 1 && green[i] == 0 && blue[i] == 1)
            Magenta++;
        else if (red[i] == 0 && green[i] == 1 && blue[i] == 1)
            Cyan++;
        else if (red[i] == 1 && green[i] == 1 && blue[i] == 0)
            Yellow++;
        else if (red[i] == 0 && green[i] == 0 && blue[i] == 1)
            Blue++;
        else if (red[i] == 0 && green[i] == 1 && blue[i] == 0)
            Green++;
        else if (red[i] == 1 && green[i] == 0 && blue[i] == 0)
            Red++;
    }
    cout << Red << " " << Green << " "
         << Blue << " " << Yellow << " ";
    cout << Cyan << " " << Magenta << " "
         << White << " " << Black << "\n";
}
int main()
{
    int n = 12r = 2g = 3b = 5;

    royAndLeds(nrgb);

    return 0;
}


No comments:

Post a Comment