Ctrl K

Restore a Ring of Bits

Medium
n bits (zeros and ones) are written around a circle. For each bit we took the XOR with its clockwise neighbor: diffs[i] is the XOR of bit i and bit i + 1, and the last bit is paired with the first. When n = 1, the bit is paired with itself. The original bits are lost; only the array diffs remains. Return True if at least one ring of bits produces exactly this diffs, and False otherwise.
Example 1
diffs=[1, 0, 1] → True

The ring [0, 1, 1] works: 0 ⊕ 1 = 1, 1 ⊕ 1 = 0, 1 ⊕ 0 = 1.

Example 2
diffs=[1, 1, 1] → False

All three neighboring pairs would have to differ, but on a ring of three bits that is impossible: coming back to the first bit, its value would have flipped an odd number of times.

Example 3
diffs=[0] → True
Constraints
  • 1 ≤ len(diffs) ≤ 10⁵
  • Every element of diffs is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Topping Combo MediumAll Distinct Card Hands MediumThe Odd One Among Triples MediumTwo Values Without a Pair MediumSpot the Extra Copy Medium
loading Python…
Input
diffs=[1, 0, 1]
Expected
True

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