Ctrl K

Eight Lamps After Many Nights

Medium
Eight street lamps stand along an alley. The array lamps describes them today: lamps[i] = 1 if lamp i is lit and 0 if it is not. Every night all lamps change at the same time according to this rule: - if a lamp has two neighbours and they are both lit or both dark, it becomes lit; - otherwise it goes dark. The two end lamps have only one neighbour each, so from the first night on they are always dark. Return the state of the lamps after nights nights.
Example 1
lamps=[1, 0, 0, 1, 1, 1, 0, 1], nights=1 → [0, 0, 0, 0, 1, 0, 1, 0]

The end lamps go dark. Lamp 1 has neighbours 1 and 0, which differ, so it goes dark; lamp 2 has 0 and 1, dark too; lamp 4 has 1 and 1, so it is lit; and so on.

Example 2
lamps=[0, 0, 0, 0, 0, 0, 0, 0], nights=3 → [0, 0, 0, 1, 1, 0, 0, 0]
Example 3
lamps=[1, 1, 0, 0, 1, 0, 1, 1], nights=1000000000 → [0, 1, 1, 1, 0, 1, 0, 0]

Simulating a billion nights one by one is too slow, but after the first night the states start repeating in a cycle.

Constraints
  • len(lamps) = 8
  • lamps[i] is 0 or 1
  • 1 ≤ nights ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Equal-XOR Halves MediumFewest Lines Through the Dots MediumThe Lost Ticket EasySquare-Sum Chains HardVertical Mirror Line Medium
loading Python…
Input
lamps=[1, 0, 0, 1, 1, 1, 0, 1], nights=1
Expected
[0, 0, 0, 0, 1, 0, 1, 0]

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