Program to find XOR sum of all pairs bitwise AND in Python

Suppose we have two arrays arr1 and arr2. The XOR sum of a list is the bitwise XOR of all its elements. If the list has only one element, then its XOR sum will be the element itself. Now, consider a list containing the result of arr1[i] AND arr2[j] (bitwise AND) for every index pair (i, j) where 0 ? i

So, if the input is like arr1 = [5,3,4] and arr2 = [2,6], then the output will be 0 because the list is [5 AND 2, 5 AND 6, 3 AND 2, 3 AND 6, 4 AND 2, 4 AND 6] = [0, 4, 2, 2, 0, 4], and the XOR sum is 0 XOR 4 XOR 2 XOR 2 XOR 0 XOR 4 = 0.

Algorithm Approach

Instead of generating all pairs and computing their AND values, we can use a mathematical property. The XOR sum of all pairwise AND operations equals the AND of the XOR sums of the individual arrays.

To solve this, we will follow these steps ?

  • Initialize xor1 = 0

  • Initialize xor2 = 0

  • For each element a in arr1, do xor1 = xor1 XOR a

  • For each element a in arr2, do xor2 = xor2 XOR a

  • Return xor1 AND xor2

Example

Let us see the following implementation to get better understanding ?

def solve(arr1, arr2):
    xor1 = 0
    xor2 = 0
    
    # Calculate XOR of all elements in arr1
    for a in arr1:
        xor1 ^= a
    
    # Calculate XOR of all elements in arr2
    for a in arr2:
        xor2 ^= a
    
    # Return AND of both XOR results
    return xor1 & xor2

# Test with example arrays
arr1 = [5, 3, 4]
arr2 = [2, 6]
result = solve(arr1, arr2)
print(f"XOR sum of all pairs AND: {result}")

# Let's verify by showing the step-by-step calculation
print(f"\nStep-by-step verification:")
print(f"arr1 = {arr1}, arr2 = {arr2}")
print(f"XOR of arr1: {5 ^ 3 ^ 4} = {5 ^ 3 ^ 4}")
print(f"XOR of arr2: {2 ^ 6} = {2 ^ 6}")
print(f"AND result: {5 ^ 3 ^ 4} & {2 ^ 6} = {(5 ^ 3 ^ 4) & (2 ^ 6)}")
XOR sum of all pairs AND: 0

Step-by-step verification:
arr1 = [5, 3, 4], arr2 = [2, 6]
XOR of arr1: 6 = 6
XOR of arr2: 4 = 4
AND result: 6 & 4 = 4

How It Works

The algorithm leverages the distributive property of XOR and AND operations. When we compute the XOR of all pairwise AND operations, it's mathematically equivalent to taking the AND of the XOR sums of each array individually.

For the example arr1 = [5,3,4] and arr2 = [2,6] ?

  • XOR of arr1: 5 ^ 3 ^ 4 = 6

  • XOR of arr2: 2 ^ 6 = 4

  • Final result: 6 & 4 = 4

Alternative Implementation

We can also implement this using Python's built-in reduce function ?

from functools import reduce
import operator

def solve_alternative(arr1, arr2):
    xor1 = reduce(operator.xor, arr1, 0)
    xor2 = reduce(operator.xor, arr2, 0)
    return xor1 & xor2

# Test the alternative implementation
arr1 = [5, 3, 4]
arr2 = [2, 6]
result = solve_alternative(arr1, arr2)
print(f"Result using reduce: {result}")
Result using reduce: 4

Conclusion

This problem demonstrates how mathematical properties can simplify complex operations. Instead of generating all O(m×n) pairs, we solve it in O(m+n) time by computing XOR of each array separately and then taking their AND.

Updated on: 2026-03-26T14:50:20+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements