Neighboring Bitwise XOR

Medium Topics Company Tags

A binary array original of length n produces derived by taking the bitwise XOR of each value with its next neighbor. The last value uses the first value as its next neighbor. Thus derived[i] = original[i] XOR original[(i + 1) % n].

Given derived, return true if any binary array original of the same length can produce it, and false otherwise. A binary array contains only 0 and 1.

Example 1:

Input: derived = [0,1,0,1]

Output: true

Explanation: An original array [0,0,1,1] produces the adjacent XOR values [0,1,0,1], including the last-to-first edge.

Example 2:

Input: derived = [1]

Output: false

Explanation: A one-element original array XORs its only value with itself, which always gives 0. Therefore derived = [1] is impossible.

Example 3:

Input: derived = [0]

Output: true

Explanation: The original array [0] produces derived = [0], so a valid original exists.

Constraints:

  • 1 <= derived.length <= 100000
  • Each value in derived is 0 or 1.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.