Loading content...
You are given two arrays of non-negative integers, nums1 and nums2. Imagine creating a third array called pairXors that contains the bitwise XOR result of every possible pairing between elements from the two arrays—that is, each element from nums1 is XOR'd with every element from nums2 exactly once.
Your task is to compute and return the aggregate XOR of all values in the pairXors array—in other words, the XOR of all pairwise XOR results.
The XOR operation (denoted by ^) is a bitwise operation that returns 1 for each bit position where the corresponding bits of the operands differ, and 0 where they are the same:
0 ^ 0 = 00 ^ 1 = 11 ^ 0 = 11 ^ 1 = 0a ^ b = b ^ a(a ^ b) ^ c = a ^ (b ^ c)a ^ a = 0a ^ 0 = aThese properties are essential to solving this problem efficiently without explicitly generating all pairs.
nums1 = [2,1,3]
nums2 = [10,2,5,0]13The pairwise XOR values are: 2^10=8, 2^2=0, 2^5=7, 2^0=2, 1^10=11, 1^2=3, 1^5=4, 1^0=1, 3^10=9, 3^2=1, 3^5=6, 3^0=3. Giving us pairXors = [8,0,7,2,11,3,4,1,9,1,6,3]. XORing all these values: 8^0^7^2^11^3^4^1^9^1^6^3 = 13.
nums1 = [1,2]
nums2 = [3,4]0The pairwise XOR values are: 1^3=2, 1^4=5, 2^3=1, 2^4=6. Thus pairXors = [2,5,1,6]. Computing 2^5^1^6 = 0.
nums1 = [5,10]
nums2 = [7]15With only one element in nums2, we get: 5^7=2 and 10^7=13. XORing these: 2^13=15.
Constraints