#2425
Medium Algorithms Bitwise xor of all pairings
Array Bit Manipulation Brainteaser
66.9% acceptance
Feb 25, 2026
920
59
You are given two 0-indexed arrays, nums1 and nums2, consisting of non-negative integers.
Let there be another array, nums3, which contains the bitwise XOR of all pairings of
integers between nums1 and nums2 (every integer in nums1 is paired with every integer
in nums2 exactly once).
Return the bitwise XOR of all integers in nums3.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn xor_all_nums(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
// Each element of nums1 appears len(nums2) times in nums3.
// Each element of nums2 appears len(nums1) times in nums3.
// XOR of x repeated even times = 0; repeated odd times = x.
let mut ans = 0;
if nums2.len() % 2 == 1 {
for &x in &nums1 {
ans ^= x;
}
}
if nums1.len() % 2 == 1 {
for &x in &nums2 {
ans ^= x;
}
}
ans
}
}