[LeetCode] Bitwise XOR of All Pairings

2425. Bitwise XOR of All Pairings

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int xorAllNums(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size(), m = nums2.size();
int res = 0;
if(n % 2 == 1) for(auto n : nums2) res ^= n;
if(m % 2 == 1) for(auto n : nums1) res ^= n;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/01/PS/LeetCode/bitwise-xor-of-all-pairings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.