Problem · Array

Count Subarrays With Given XOR

Learn this problem
MediumInMobi logoInMobiNEW GRADOA

Problem 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) → int

Examples

Example 1

arr = [5,3,8,3,10]x = 8return = 2

The qualifying subarrays are [3, 8, 3] and [8].

Example 2

arr = [5,2,9]x = 7return = 1

Only the subarray [5, 2] has XOR 7.

Example 3

arr = [0,0,0]x = 0return = 6

Every non-empty contiguous subarray has XOR 0, and an array of length 3 has 6 such subarrays.

Constraints

  • 3 <= arr.length <= 5 * 10^4
  • 0 <= arr[i] <= 10^9
  • 0 <= x <= 10^9

More InMobi problems

drafts saved locally
public int subarraysXor(int[] arr, int x) {
    // write your code here.
}
arr[5,3,8,3,10]
x8
expected2
checking account