Skip to main content
Back to problems
#2683
Medium Algorithms

Neighboring bitwise xor

Array Bit Manipulation
79.8% acceptance
Feb 25, 2026
803
50
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn does_valid_array_exist(derived: Vec<i32>) -> bool {
    // XOR of all derived must be 0
    derived.iter().fold(0, |acc, &x| acc ^ x) == 0
  }
}