Bitwise XOR of All Pairings - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums1 = [2,1], nums2 = [3,4]
Output: 2
💡 Note: All pairs: (2,3)→1, (2,4)→6, (1,3)→2, (1,4)→5. Final XOR: 1⊕6⊕2⊕5 = 2
Example 2 — Single Elements
$ Input: nums1 = [1], nums2 = [2]
Output: 3
💡 Note: Only one pair: (1,2) → 1⊕2 = 3
Example 3 — Same Values
$ Input: nums1 = [1,2], nums2 = [3,4]
Output: 0
💡 Note: Pairs: (1,3)→2, (1,4)→5, (2,3)→1, (2,4)→6. XOR: 2⊕5⊕1⊕6 = 0

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 105
  • 0 ≤ nums1[i], nums2[j] ≤ 109

Visualization

Tap to expand
Bitwise XOR of All Pairingsnums1 = [2, 1]21nums2 = [3, 4]34All Possible Pairs:2⊕3 = 12⊕4 = 61⊕3 = 21⊕4 = 5XOR All Results:1 ⊕ 6 ⊕ 2 ⊕ 5 = 2
Understanding the Visualization
1
Input Arrays
Two arrays nums1=[2,1] and nums2=[3,4]
2
Generate All Pairs
Create pairs: (2,3), (2,4), (1,3), (1,4) and XOR each
3
Final XOR
XOR all pair results: 1⊕6⊕2⊕5 = 2
Key Takeaway
🎯 Key Insight: XOR cancellation property allows us to avoid generating all pairs by analyzing element frequencies.
Asked in
Google 15 Microsoft 12 Amazon 8
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen