Circular Lamps with Traveling Togglers
Arrays & HashingAuto-graded · 20 test casesLast updated
Find the final bits on a ring after several togglers walk clockwise for k steps, using lap decomposition, parity, and a cyclic difference array.
Circular Lamps with Traveling Togglers
A ring has n lamps, numbered 0 to n - 1 clockwise. After lamp n - 1 comes lamp 0. Each lamp is either off (0) or on (1), and colors gives the starting states.
You are also given a list markers of starting positions for m togglers. Several togglers may start on the same lamp. You are also given an integer k, the number of rounds to run.
Each round has two phases:
- Every toggler flips the lamp it is standing on. A lamp hosting
ttogglers is flippedttimes in this phase, so it changes state only iftis odd. - Then all togglers move one lamp clockwise at the same time. A toggler on lamp
pmoves to lamp(p + 1) % n.
Run exactly k rounds and return the final state of all n lamps as a list. Do not modify the input list.
If k = 0, nothing happens and the result equals colors.
Constraints
1 <= n <= 100,000len(colors) == n, and eachcolors[i]is0or11 <= len(markers) <= 100,0000 <= markers[i] < nkis a non-negative integer that can be very large (up to about10^18), so simulating round by round is too slow- The return value is a list of
nintegers, each0or1
Examples
Example 1
Input: toggle_ring_lamps(4, [0, 0, 0, 0], [0, 2], 3)
Output: [0, 1, 0, 1]
Example 2
Input: toggle_ring_lamps(5, [1, 0, 1, 1, 0], [1], 5)
Output: [0, 1, 0, 0, 1]
Hints
Hint 1
After n rounds a single toggler has stood on every lamp exactly once. Write k = q * n + r and treat the q full laps separately from the leftover r rounds.
Hint 2
Only the parity of the number of flips per lamp matters. Full laps flip every lamp q * m times in total, so they invert everything only if q and m are both odd.
Hint 3
In the leftover r rounds (r < n), a toggler starting at p flips the cyclic range p, p+1, ..., p+r-1. Count how many ranges cover each lamp with a difference array, splitting ranges that wrap past n-1, then take each count mod 2.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Simulating every round costs O(k * m), which is far too slow when k is huge. Two observations remove the dependence on k.
- Only parity matters. A lamp's final state is its initial state XOR the parity of the number of times it was flipped.
- Laps are uniform. A toggler starting at
pstands on positionsp, p+1, ..., p+k-1(modn) during the flip phases of rounds0 .. k-1. Writek = q * n + rwith0 <= r < n.- The first
q * nvisits formqcomplete laps. Each lap visits every lamp exactly once. Across allmtogglers, every lamp is flippedq * mtimes, so all lamps invert exactly whenqandmare both odd. - The last
rvisits cover the cyclic rangep, p+1, ..., p+r-1. Sincer < n, no lamp is visited twice in this range.
- The first
So after the lap step, we need the number of cyclic ranges of length r that cover each lamp, taken mod 2.
Counting coverage
Use a difference array diff of size n + 1. For a toggler at p with end = p + r:
- If
end <= n, dodiff[p] += 1anddiff[end] -= 1. - Otherwise the range wraps. Split it into
[p, n)and[0, end - n):diff[p] += 1,diff[n] -= 1,diff[0] += 1,diff[end - n] -= 1.
A prefix sum over diff gives the coverage count for each lamp. XOR the lamp with that count's lowest bit.
Why it is correct
- Flips commute (XOR), so the order within a round does not matter. Stacked togglers simply add several flips to the same lamp.
- The lap argument shows each lamp receives exactly
qflips per toggler from the full laps, and the partial range gives exactly one extra flip to each lamp in[p, p+r)and none to the others. - The difference array counts the ranges covering each lamp exactly, and only the parity is applied.
- If
k = 0, thenq = r = 0and the input is returned unchanged.
Complexity
- Time:
O(n + m), independent ofk(divmodon a big integer is effectively constant here). - Space:
O(n)for the difference array and the output.
Reference solution
from typing import List
def toggle_ring_lamps(n: int, colors: List[int], markers: List[int], k: int) -> List[int]:
result = list(colors)
laps, rem = divmod(k, n)
m = len(markers)
# Full laps: each lamp is flipped laps * m times.
if (laps % 2) and (m % 2):
result = [b ^ 1 for b in result]
if rem:
diff = [0] * (n + 1)
for p in markers:
end = p + rem
diff[p] += 1
if end <= n:
diff[end] -= 1
else:
# wraps: [p, n) and [0, end - n)
diff[n] -= 1
diff[0] += 1
diff[end - n] -= 1
running = 0
for i in range(n):
running += diff[i]
result[i] ^= running & 1
return result