Find XOR Sum of All Pairs Bitwise AND - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
arr1 = [1,2], arr2 = [3,4,5]
›
Output:
2
💡 Note:
All pairs: (1&3)=1, (1&4)=0, (1&5)=1, (2&3)=2, (2&4)=0, (2&5)=0. XOR sum: 1⊕0⊕1⊕2⊕0⊕0 = 2
Example 2 — Single Elements
$
Input:
arr1 = [12], arr2 = [4]
›
Output:
4
💡 Note:
Only one pair: 12 & 4 = 4. XOR sum of [4] = 4
Example 3 — All Zero Results
$
Input:
arr1 = [1], arr2 = [2,4]
›
Output:
0
💡 Note:
Pairs: (1&2)=0, (1&4)=0. XOR sum: 0⊕0 = 0
Constraints
- 1 ≤ arr1.length, arr2.length ≤ 105
- 0 ≤ arr1[i], arr2[j] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
Two arrays arr1=[1,2] and arr2=[3,4,5]
2
Generate AND Pairs
Compute arr1[i] & arr2[j] for all i,j combinations
3
XOR Sum
XOR all AND results: 1⊕0⊕1⊕2⊕0⊕0 = 2
Key Takeaway
🎯 Key Insight: Generate all possible AND pairs between arrays, then XOR all results together
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code