FastPrepCount Prefix Subsets with a Given XOR

Count Prefix Subsets with a Given XOR

GoDaddy logoGoDaddy● HardFULLTIMEOA

Problem statement

You are given an integer array arr and a list of queries. Each query is [end, k].

For each query, count the subsets of positions in the inclusive prefix arr[0...end] whose selected values have bitwise XOR equal to k. The empty subset is allowed and has XOR 0. Positions are distinct choices, even when they contain the same value.

Return one count per query in the original query order. Return each count modulo 10^9 + 7. Queries are independent and do not change the array.

Function

countPrefixXorSubsets(arr: int[], queries: int[][]) → int[]

Examples

Example 1

arr = [1,2,3]queries = [[1,3],[2,0]]return = [1,2]

For [1, 3], the prefix is [1, 2], and only the subset containing both positions has XOR 3.

For [2, 0], the prefix is [1, 2, 3]. The empty subset and the subset containing all three positions have XOR 0, so the count is 2.

Example 2

arr = [0,1,1]queries = [[0,0],[2,1],[1,2]]return = [2,4,0]

In the first prefix, both the empty subset and the subset containing the zero have XOR 0. In the full array, choose exactly one of the two positions containing 1, then choose whether to include the zero, giving 2 * 2 = 4 subsets. Target 2 is unreachable from [0, 1].

Constraints

  • 1 <= arr.length <= 10^5.
  • 1 <= queries.length <= 10^5.
  • Each query contains exactly 2 integers: [end, k].
  • Every array value and target is between 0 and 2^30 - 1, inclusive.
  • Every query satisfies 0 <= end < arr.length.

More GoDaddy problems

See GoDaddy hiring insights
Saved
public int[] countPrefixXorSubsets(int[] arr, int[][] queries) {
    // write your code here
}
Checking account…