Count Distinct Bulb Configurations After Exact Presses

You are given n bulbs arranged in a row and labeled from 1 to n. Initially, all bulbs are turned on.

There are 4 buttons, and each button flips the state of a specific set of bulbs:

  • Button 1: Flip all bulbs.
  • Button 2: Flip all bulbs with even-numbered labels.
  • Button 3: Flip all bulbs with odd-numbered labels.
  • Button 4: Flip all bulbs whose label is of the form 3k + 1 for some non-negative integer k (that is: 1, 4, 7, 10, ...).

You must perform exactly presses button presses.

In each press, you may choose any one of the 4 buttons.

Return the number of distinct final bulb states possible after performing exactly presses presses.

Example 1:

Input: n = 1, presses = 2

Output: 2

Explanation:

There is only one bulb.

Possible final states are:

  • [on]
  • [off]

So, the total number of distinct final states is 2.

Example 2:

Input: n = 2, presses = 2

Output: 4

Explanation:

The possible distinct final states are:

  • [on, on]
  • [off, off]
  • [on, off]
  • [off, on]

So, the answer is 4.

Now Your Turn!

Pick the correct output for the given input

Input: n = 3, presses = 2

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 1000
  • 0 <= presses <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int flipLights(int n, int presses) {
// Your code goes here
}
};
Test Case

Input:

N
Presses