[LeetCode] Neighboring Bitwise XOR

2683. Neighboring Bitwise XOR

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
1
2
3
4
5
6
class Solution {
public:
bool doesValidArrayExist(vector<int>& A) {
return accumulate(begin(A), end(A), 0, bit_xor<void>()) == 0;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/14/PS/LeetCode/neighboring-bitwise-xor/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.