Class Solution
-
- All Implemented Interfaces:
public final class Solution2683 - Neighboring Bitwise XOR.
Medium
A 0-indexed array
derivedwith lengthnis derived by computing the bitwise XOR (⊕) of adjacent values in a binary arrayoriginalof lengthn.Specifically, for each index
iin the range[0, n - 1]:If
i = n - 1, thenderived[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 arrayoriginalthat could have formedderived.Return true if such an array exists or false otherwise.
A binary array is an array containing only 0's and 1's
Example 1:
Input: derived = 1,1,0
Output: true
Explanation: A valid original array that gives derived is 0,1,0.
derived0 = original0 ⊕ original1 = 0 ⊕ 1 = 1
derived1 = original1 ⊕ original2 = 1 ⊕ 0 = 1
derived2 = original2 ⊕ original0 = 0 ⊕ 0 = 0
Example 2:
Input: derived = 1,1
Output: true
Explanation: A valid original array that gives derived is 0,1.
derived0 = original0 ⊕ original1 = 1
derived1 = original1 ⊕ original0 = 1
Example 3:
Input: derived = 1,0
Output: false
Explanation: There is no valid original array that gives derived.
Constraints:
n == derived.length<code>1 <= n <= 10<sup>5</sup></code>
The values in
derivedare either 0's or 1's
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleandoesValidArrayExist(IntArray derived)-
-
Method Detail
-
doesValidArrayExist
final Boolean doesValidArrayExist(IntArray derived)
-
-
-
-