Ctrl K

Back to a Dark Panel

Medium
A panel holds a row of switches numbered from zero. Each number in presses describes one press: if the number's binary form has a 1 in bit b, the press flips switch b (on ↔ off). Bit 0 is the least significant. Take a contiguous segment of presses and run it on a panel with every switch off. The segment is a *reset* if the panel ends up fully off again. Return how many reset segments presses contains.
Example 1
presses=[5, 3, 6, 1] → 1

5 = 101₂, 3 = 011₂, 6 = 110₂. The presses [5, 3, 6] flip every switch exactly twice, so the panel is dark again. No other segment does that.

Example 2
presses=[0, 4, 4] → 3

[0], [4, 4] and [0, 4, 4] qualify.

Example 3
presses=[1, 2, 4] → 0
Constraints
  • 1 ≤ len(presses) ≤ 10⁵
  • 0 ≤ presses[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPalindrome After a Few Fixes MediumSplit Into Equal-XOR Halves MediumLongest Run with a Given Sum MediumMost Different Pair MediumAll Non-Falling Subsequences Medium
loading Python…
Input
presses=[5, 3, 6, 1]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 119 tests on the server, hidden ones included.