Find XOR Sum of All Pairs Bitwise AND LeetCode solution in C++

1
class Solution {
2
public:
3
// this code is basically pure mathematics, mainly distributive property with
4
// AND and XOR
5

6
int getXORSum(vector<int> &arr1, vector<int> &arr2) {
7
int m = arr1.size();
8
int n = arr2.size();
9
long int ans1 = 0, ans2 = 0;
10
for (int i = 0; i < m; i++) // this loop stores the XOR between every element of arr1
11
{
12
ans1 = ans1 ^ arr1[i];
13
}
14
for (int j = 0; j < n; j++) // this loop stores the XOR between every element of arr2
15
{
16
ans2 = ans2 ^ arr2[j];
17
}
18
return ans1 & ans2; // AND operation of both XOR's is the answer.
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0