There is a room with n bulbs labeled from 1 to n that all are turned on initially, and four buttons on the wall. Each of the four buttons has a different functionality:

Button 1: Flips the status of all the bulbs.

Button 2: Flips the status of all the bulbs with even labels (i.e., 2, 4, 6, ...).

Button 3: Flips the status of all the bulbs with odd labels (i.e., 1, 3, 5, ...).

Button 4: Flips the status of all the bulbs with labels j = 3k + 1 where k = 0, 1, 2, ... (i.e., 1, 4, 7, 10, ...).

You must make exactly presses button presses in total. For each press, you may pick any of the four buttons to press.

Given the two integers n and presses, return the number of different possible statuses after performing all presses button presses.

Input & Output

Example 1 — Small Case
$ Input: n = 1, presses = 1
Output: 4
💡 Note: With 1 bulb and 1 press, each of the 4 buttons creates a different state: Button 1 turns it off, Button 2 keeps it on (no effect on bulb 1), Button 3 turns it off, Button 4 turns it off. So we have states [0], [1], [0], [0] - but only 2 unique states, actually it's 4 different button choices.
Example 2 — Two Bulbs
$ Input: n = 2, presses = 1
Output: 4
💡 Note: With 2 bulbs [1,1] initially: Button 1 → [0,0], Button 2 → [1,0], Button 3 → [0,1], Button 4 → [0,1]. We get 4 different final states.
Example 3 — Multiple Presses
$ Input: n = 3, presses = 2
Output: 7
💡 Note: With 3 bulbs and 2 presses, we can combine buttons in different ways. Due to XOR properties, pressing the same button twice has no effect, so we focus on combinations of different buttons.

Constraints

  • 1 ≤ n ≤ 1000
  • 0 ≤ presses ≤ 1000

Visualization

Tap to expand
Bulb Switcher II: Button Effects on BulbsInitial state: All bulbs ON, find unique states after button presses111Bulb 1Bulb 2Bulb 3Initial State: [1,1,1]Button 1: AllButton 2: EvenButton 3: OddButton 4: 3k+1[0,0,0][1,0,1][0,1,0][0,1,1]For n=3, presses=1: 4 different possible statesKey: Button combinations create different patternsMathematical insight: Only parity of button presses matters
Understanding the Visualization
1
Initial State
All bulbs start ON: [1,1,1]
2
Button Effects
Each button flips different bulb patterns
3
Count Unique
Find number of distinct final states possible
Key Takeaway
🎯 Key Insight: Button effects are XOR operations - pressing the same button twice cancels out, so only the parity matters
Asked in
Google 15 Microsoft 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen