Suppose there is a room with n lights which are switched on initially and 4 buttons present on the wall. After performing exactly m unknown operations towards buttons, we need to return how many different kinds of status of the n lights could be. So consider n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are as follows −
Now if n = 3 and m = 1, then there will be 4 operations, these are, [off, on, off], [on, off, on], [off, off, off], [off, on, on]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int flipLights(int n, int m) { if (m == 0 || n == 0) return 1; if (n == 1) return 2; if (n == 2) return m == 1? 3:4; if (m == 1) return 4; return m == 2? 7:8; } }; main(){ Solution ob; cout << (ob.flipLights(3, 1)); }
3 1
4