1835. Find XOR Sum of All Pairs Bitwise AND LeetCode Solution

In this guide, you will get 1835. Find XOR Sum of All Pairs Bitwise AND LeetCode Solution with the best time and space complexity. The solution to Find XOR Sum of All Pairs Bitwise AND problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find XOR Sum of All Pairs Bitwise AND solution in C++
  4. Find XOR Sum of All Pairs Bitwise AND solution in Java
  5. Find XOR Sum of All Pairs Bitwise AND solution in Python
  6. Additional Resources
1835. Find XOR Sum of All Pairs Bitwise AND LeetCode Solution image

Problem Statement of Find XOR Sum of All Pairs Bitwise AND

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.
Return the XOR sum of the aforementioned list.

Example 1:

Input: arr1 = [1,2,3], arr2 = [6,5]
Output: 0
Explanation: 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.

Example 2:

Input: arr1 = [12], arr2 = [4]
Output: 4
Explanation: The list = [12 AND 4] = [4]. The XOR sum = 4.

Constraints:

1 <= arr1.length, arr2.length <= 105
0 <= arr1[i], arr2[j] <= 109

Complexity Analysis

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

1835. Find XOR Sum of All Pairs Bitwise AND LeetCode Solution in C++

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;
  }
};
/* code provided by PROGIEZ */

1835. Find XOR Sum of All Pairs Bitwise AND LeetCode Solution in 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;
  }
}
// code provided by PROGIEZ

1835. Find XOR Sum of All Pairs Bitwise AND LeetCode Solution in Python

class Solution:
  def getXORSum(self, arr1: list[int], arr2: list[int]) -> int:
    return functools.reduce(
        operator.xor, arr1) & functools.reduce(
        operator.xor, arr2)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.