Problem · Array
Count Subarrays With Given XOR
Learn this problemProblem statement
You are given an integer array arr and an integer x.
Return the number of non-empty contiguous subarrays whose bitwise XOR is exactly x.
Function
subarraysXor(arr: int[], x: int) → intExamples
Example 1
arr = [5,3,8,3,10]x = 8return = 2The qualifying subarrays are [3, 8, 3] and [8].
Example 2
arr = [5,2,9]x = 7return = 1Only the subarray [5, 2] has XOR 7.
Example 3
arr = [0,0,0]x = 0return = 6Every non-empty contiguous subarray has XOR 0, and an array of length 3 has 6 such subarrays.
Constraints
3 <= arr.length <= 5 * 10^40 <= arr[i] <= 10^90 <= x <= 10^9