The XOR sum of a list is the bitwise XOR of all its elements. If the list only contains one element, then its XOR sum will be equal to this element.
For example, the XOR sum of [1,2,3,4] is equal to 1 XOR 2 XOR 3 XOR 4 = 4, and the XOR sum of [3] is equal to 3.
You are given two 0-indexed arrays arr1 and arr2 that consist only of non-negative integers.
Consider the list containing the result of arr1[i] AND arr2[j] (bitwise
AND) for every (i, j) pair where 0 <= i < arr1.length and 0 <= j < arr2.length.
Input: arr1 =[1,2,3], arr2 =[6,5]Output: 0Explanation: The list =[1 AND 6,1 AND 5,2 AND 6,2 AND 5,3 AND 6,3 AND 5]=[0,1,2,0,2,1].The XOR sum =0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1=0.
classSolution {
public:int xorSum(vector<int>& arr1, vector<int>& arr2) {
int x1 =0, x2 =0;
for (int a : arr1) x1 ^= a;
for (int b : arr2) x2 ^= b;
return x1 & x2;
}
};
1
2
3
4
5
6
funcxorSum(arr1, arr2 []int) int {
x1, x2:=0, 0for_, a:=rangearr1 { x1 ^= a }
for_, b:=rangearr2 { x2 ^= b }
returnx1&x2}
1
2
3
4
5
6
7
8
classSolution {
publicintxorSum(int[] arr1, int[] arr2) {
int x1 = 0, x2 = 0;
for (int a : arr1) x1 ^= a;
for (int b : arr2) x2 ^= b;
return x1 & x2;
}
}
1
2
3
4
5
6
7
8
classSolution {
funxorSum(arr1: IntArray, arr2: IntArray): Int {
var x1 = 0; var x2 = 0for (a in arr1) x1 = x1 xor a
for (b in arr2) x2 = x2 xor b
return x1 and x2
}
}
1
2
3
4
5
6
7
classSolution:
defxorSum(self, arr1: list[int], arr2: list[int]) -> int:
x1 =0for a in arr1: x1 ^= a
x2 =0for b in arr2: x2 ^= b
return x1 & x2