Imagine you're in a room with n bulbs labeled from 1 to n, all initially turned ON. On the wall, there are four special buttons that control the bulbs in different patterns:

  • Button 1: Flips all bulbs (1, 2, 3, 4, ...)
  • Button 2: Flips bulbs with even labels (2, 4, 6, 8, ...)
  • Button 3: Flips bulbs with odd labels (1, 3, 5, 7, ...)
  • Button 4: Flips bulbs at positions 3k + 1 where k โ‰ฅ 0 (1, 4, 7, 10, ...)

You must press buttons exactly presses times. Each press can be any of the four buttons. Your goal is to determine how many different possible states the bulbs can be in after all button presses.

Example: With n=2 and presses=1, you could press button 1 (both bulbs OFF), button 2 (bulb 1 ON, bulb 2 OFF), or button 3 (bulb 1 OFF, bulb 2 ON), giving you 3 different states.

Input & Output

example_1.py โ€” Basic case
$ Input: n = 1, presses = 1
โ€บ Output: 4
๐Ÿ’ก Note: With 1 bulb and 1 press: Button 1 โ†’ [0], Button 2 โ†’ [1] (no effect), Button 3 โ†’ [0], Button 4 โ†’ [0]. All 4 buttons give different results.
example_2.py โ€” Two bulbs
$ Input: n = 2, presses = 1
โ€บ Output: 3
๐Ÿ’ก Note: Button 1 โ†’ [0,0], Button 2 โ†’ [1,0], Button 3 โ†’ [0,1], Button 4 โ†’ [0,1]. Buttons 3 and 4 give same result, so 3 unique states.
example_3.py โ€” Multiple presses
$ Input: n = 3, presses = 2
โ€บ Output: 7
๐Ÿ’ก Note: With 2 presses, we can achieve combinations like pressing different buttons once each, or pressing one button twice (which cancels out). This creates 7 distinct final states.

Visualization

Tap to expand
Smart Lighting Control SystemInitial State:123Control PanelButton 1All BulbsButton 2Even PosButton 3Odd PosButton 43k+1 PosEffects: 1:[1,2,3] 2:[2] 3:[1,3] 4:[1]Pattern repeats every 3 positions!Key Discovery: Mathematical Patterns๐Ÿ” Button 1 (All):Affects positions: 1,2,3,4,5,6,... (period=1)๐Ÿ” Button 2 (Even):Affects positions: 2,4,6,8,10,... (period=2)๐Ÿ” Button 3 (Odd):Affects positions: 1,3,5,7,9,... (period=2)๐Ÿ” Button 4 (3k+1):Affects positions: 1,4,7,10,13,... (period=3)๐Ÿ’ก LCM(1,2,2,3) = 6, but first 3 positions determine everything!Optimization: O(4^presses) โ†’ O(1)โœจ Insight 1: Same button twice cancels โ†’ only parity matters (16 combinations)โœจ Insight 2: Pattern repeats โ†’ only first 3 bulbs neededโœจ Result: Check 16 states in constant time regardless of n or presses!๐Ÿš€ From exponential to constant time complexity!
Understanding the Visualization
1
Pattern Recognition
Notice that button effects follow mathematical patterns - even/odd positions, arithmetic sequences
2
Cancellation Discovery
Realize that pressing same button twice returns to original state
3
Periodicity Insight
Understand that the pattern repeats every 3 bulbs due to LCM of button patterns
4
State Space Reduction
Reduce from exponential to constant time by focusing on button press parities
Key Takeaway
๐ŸŽฏ Key Insight: Mathematical pattern recognition transforms an exponential problem into a constant-time solution by identifying button press cancellations and periodic bulb patterns.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Constant time as we only check fixed number of combinations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only store a few button states and bulb configurations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • 0 โ‰ค presses โ‰ค 1000
  • All bulbs start in the ON state
  • Button presses must be exactly equal to the given number
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
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