FastPrepSum of XOR Across Cartesian Pairs
Problem · Array

Sum of XOR Across Cartesian Pairs

Learn this problem
MediumIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Given two arrays of nonnegative integers, left and right, consider every ordered pair (left[i], right[j]).

Return the sum of left[i] XOR right[j] over all such pairs.

Function

sumPairXor(left: int[], right: int[]) → long

Examples

Example 1

left = [1,2]right = [3,4]return = 14

The four XOR values are 1 XOR 3 = 2, 1 XOR 4 = 5, 2 XOR 3 = 1, and 2 XOR 4 = 6. Their sum is 14.

Example 2

left = [0,7]right = [7]return = 7

The pair values are 0 XOR 7 = 7 and 7 XOR 7 = 0.

Constraints

  • 1 <= left.length, right.length <= 20000
  • 0 <= left[i], right[j] <= 10^9
  • The answer fits in a signed 64-bit integer.

More IBM problems

drafts saved locally
public long sumPairXor(int[] left, int[] right) {
    // Write your code here
}
left[1,2]
right[3,4]
expected14
checking account