- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/bitwise-xor-of-all-pairings
- topics: Bit Manipulation
You are given two 0-indexed arrays, nums1
and nums2
, consisting of non-negative integers. There exists 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
.
Idea
Visualize the matrix and observe that we have same elements repeated n/m times where n/m are the lengths of the lists
class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
ans = 0
if len(nums1) % 2 == 1:
for a in nums2:
ans ^= a
if len(nums2) % 2 == 1:
for a in nums1:
ans ^= a
return ans