Neighboring Bitwise XOR - Problem

A 0-indexed array derived with length n is derived by computing the bitwise XOR (⊕) of adjacent values in a binary array original of length n.

Specifically, for each index i in the range [0, n - 1]:

  • If i = n - 1, then derived[i] = original[i] ⊕ original[0].
  • Otherwise, derived[i] = original[i] ⊕ original[i + 1].

Given an array derived, your task is to determine whether there exists a valid binary array original that could have formed derived.

Return true if such an array exists or false otherwise.

A binary array is an array containing only 0's and 1's

Input & Output

Example 1 — Valid Case
$ Input: derived = [1,1,0]
Output: true
💡 Note: Original array [0,1,0] produces derived [1,1,0]: 0⊕1=1, 1⊕0=1, 0⊕0=0
Example 2 — Invalid Case
$ Input: derived = [1,0,0]
Output: false
💡 Note: No binary array can produce [1,0,0]. XOR sum = 1⊕0⊕0 = 1 ≠ 0
Example 3 — All Zeros
$ Input: derived = [0,0,0,0]
Output: true
💡 Note: Original [0,0,0,0] works: all XOR operations yield 0

Constraints

  • 1 ≤ derived.length ≤ 105
  • derived[i] is either 0 or 1

Visualization

Tap to expand
Neighboring Bitwise XOR ProblemStep 1: Given Derived Array110Step 2: Find Original That Produces This010originalarray[0,1,0]Verify: 0⊕1=1, 1⊕0=1, 0⊕0=0 ✓Valid original found → Return true
Understanding the Visualization
1
Input
Given derived array [1,1,0]
2
Process
Check if valid original exists
3
Output
Return true/false
Key Takeaway
🎯 Key Insight: XOR sum of all derived values must be 0 for valid solution
Asked in
Google 15 Microsoft 12 Amazon 8
13.1K Views
Medium Frequency
~15 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen