Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 where:
2, 4, ...).1, 3, ...).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.
Example 1:
Input: n = 1, presses = 1 Output: 2 Explanation: Status can be: - [off] by pressing button 1 - [on] by pressing button 2
Example 2:
Input: n = 2, presses = 1 Output: 3 Explanation: Status can be: - [off, off] by pressing button 1 - [on, off] by pressing button 2 - [off, on] by pressing button 3
Example 3:
Input: n = 3, presses = 1 Output: 4 Explanation: Status can be: - [off, off, off] by pressing button 1 - [on, off, on] by pressing button 2 - [off, on, off] by pressing button 3 - [off, on, on] by pressing button 4
Constraints:
1 <= n <= 10000 <= presses <= 1000Problem summary: 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 where: 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, ...). Button 3: Flips the status of all the bulbs with odd labels (i.e., 1, 3, ...). Button 4: Flips the status of all the bulbs with a label 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Bit Manipulation
1 1
2 1
3 1
bulb-switcher)number-of-times-binary-string-is-prefix-aligned)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #672: Bulb Switcher II
class Solution {
public int flipLights(int n, int presses) {
int[] ops = new int[] {0b111111, 0b010101, 0b101010, 0b100100};
Set<Integer> vis = new HashSet<>();
n = Math.min(n, 6);
for (int mask = 0; mask < 1 << 4; ++mask) {
int cnt = Integer.bitCount(mask);
if (cnt <= presses && cnt % 2 == presses % 2) {
int t = 0;
for (int i = 0; i < 4; ++i) {
if (((mask >> i) & 1) == 1) {
t ^= ops[i];
}
}
t &= ((1 << 6) - 1);
t >>= (6 - n);
vis.add(t);
}
}
return vis.size();
}
}
// Accepted solution for LeetCode #672: Bulb Switcher II
func flipLights(n int, presses int) int {
if n > 6 {
n = 6
}
ops := []int{0b111111, 0b010101, 0b101010, 0b100100}
vis := map[int]bool{}
for mask := 0; mask < 1<<4; mask++ {
cnt := bits.OnesCount(uint(mask))
if cnt <= presses && cnt%2 == presses%2 {
t := 0
for i, op := range ops {
if mask>>i&1 == 1 {
t ^= op
}
}
t &= 1<<6 - 1
t >>= (6 - n)
vis[t] = true
}
}
return len(vis)
}
# Accepted solution for LeetCode #672: Bulb Switcher II
class Solution:
def flipLights(self, n: int, presses: int) -> int:
ops = (0b111111, 0b010101, 0b101010, 0b100100)
n = min(n, 6)
vis = set()
for mask in range(1 << 4):
cnt = mask.bit_count()
if cnt <= presses and cnt % 2 == presses % 2:
t = 0
for i, op in enumerate(ops):
if (mask >> i) & 1:
t ^= op
t &= (1 << 6) - 1
t >>= 6 - n
vis.add(t)
return len(vis)
// Accepted solution for LeetCode #672: Bulb Switcher II
struct Solution;
impl Solution {
fn flip_lights(n: i32, m: i32) -> i32 {
let n = n.min(3);
if m == 0 || n == 0 {
return 1;
}
if n == 1 {
return 2;
}
if n == 2 {
return if m == 1 { 3 } else { 4 };
}
if m == 1 {
return 4;
}
if m == 2 {
7
} else {
8
}
}
}
#[test]
fn test() {
let n = 1;
let m = 1;
let res = 2;
assert_eq!(Solution::flip_lights(n, m), res);
let n = 2;
let m = 1;
let res = 3;
assert_eq!(Solution::flip_lights(n, m), res);
let n = 3;
let m = 1;
let res = 4;
assert_eq!(Solution::flip_lights(n, m), res);
}
// Accepted solution for LeetCode #672: Bulb Switcher II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #672: Bulb Switcher II
// class Solution {
// public int flipLights(int n, int presses) {
// int[] ops = new int[] {0b111111, 0b010101, 0b101010, 0b100100};
// Set<Integer> vis = new HashSet<>();
// n = Math.min(n, 6);
// for (int mask = 0; mask < 1 << 4; ++mask) {
// int cnt = Integer.bitCount(mask);
// if (cnt <= presses && cnt % 2 == presses % 2) {
// int t = 0;
// for (int i = 0; i < 4; ++i) {
// if (((mask >> i) & 1) == 1) {
// t ^= ops[i];
// }
// }
// t &= ((1 << 6) - 1);
// t >>= (6 - n);
// vis.add(t);
// }
// }
// return vis.size();
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.