#1835
Hard Algorithms Find xor sum of all pairs bitwise and
Array Math Bit Manipulation
62.5% acceptance
Feb 25, 2026
627
51
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.
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn get_xor_sum(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
let xor1 = arr1.iter().fold(0, |acc, &x| acc ^ x);
let xor2 = arr2.iter().fold(0, |acc, &x| acc ^ x);
xor1 & xor2
}
}