Leetcode

Find XOR Sum of All Pairs Bitwise AND

  • Time:O(n)
  • Space:O(1)

C++

class Solution {
 public:
  int getXORSum(vector<int>& arr1, vector<int>& arr2) {
    const int xors1 = accumulate(begin(arr1), end(arr1), 0, bit_xor<>());
    const int xors2 = accumulate(begin(arr2), end(arr2), 0, bit_xor<>());
    return xors1 & xors2;
  }
};

JAVA

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;
  }
}

Python

class Solution:
  def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
    return reduce(operator.xor, arr1) & reduce(operator.xor, arr2)