Skip to content

1835. Find XOR Sum of All Pairs Bitwise AND 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
class Solution {
 public:
  int getXORSum(vector<int>& arr1, vector<int>& arr2) {
    const int xors1 = accumulate(arr1.begin(), arr1.end(), 0, bit_xor<>());
    const int xors2 = accumulate(arr2.begin(), arr2.end(), 0, bit_xor<>());
    return xors1 & xors2;
  }
};
1
2
3
4
5
6
7
class Solution {
  public int getXORSum(int[] arr1, int[] arr2) {
    final int xors1 = Arrays.stream(arr1).reduce((a, b) -> a ^ b).getAsInt();
    final int xors2 = Arrays.stream(arr2).reduce((a, b) -> a ^ b).getAsInt();
    return xors1 & xors2;
  }
}
1
2
3
class Solution:
  def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
    return functools.reduce(operator.xor, arr1) & functools.reduce(operator.xor, arr2)