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 n, int r, int g, int b){vector<int> red, green, blue;int i = 0, cntr = 0, cntg = 0, cntb = 0;int flag = 0;while (i < n){red.push_back(flag);cntr++;i++;if (cntr == r){flag = !flag;cntr = 0;}}flag = 0, i = 0;while (i < n){green.push_back(flag);cntg++;i++;if (cntg == g){flag = !flag;cntg = 0;}}flag = 0, i = 0;while (i < n){blue.push_back(flag);cntb++;i++;if (cntb == b){flag = !flag;cntb = 0;}}int Red = 0, Green = 0, Blue = 0;int Yellow = 0, Cyan = 0, Magenta = 0;int White = 0, Black = 0;for (int i = 0; i < n; i++){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 = 12, r = 2, g = 3, b = 5;royAndLeds(n, r, g, b);return 0;}
No comments:
Post a Comment