diff options
author | Jack Sangdahl <[email protected]> | 2025-01-15 19:40:45 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-01-15 19:40:45 -0700 |
commit | 14aac43ae43413bb8d6d04bee8c342df2e8b2669 (patch) | |
tree | c855d7974eb91b5aa26ab479fc42547ea2020b30 | |
parent | 739d4b1bff93a46887828a958d85c4a0a0bd6c71 (diff) | |
download | leetcode-14aac43ae43413bb8d6d04bee8c342df2e8b2669.tar.gz leetcode-14aac43ae43413bb8d6d04bee8c342df2e8b2669.zip |
2425. bitwise xor of all pairings
-rw-r--r-- | bitwise_xor_pairings.c | 28 | ||||
-rw-r--r-- | bitwise_xor_pairings.py | 29 |
2 files changed, 57 insertions, 0 deletions
diff --git a/bitwise_xor_pairings.c b/bitwise_xor_pairings.c new file mode 100644 index 0000000..9740d7e --- /dev/null +++ b/bitwise_xor_pairings.c @@ -0,0 +1,28 @@ +// 2425. Bitwise XOR of All Pairings +#include "leetcode.h" + +/* + * 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' + */ + +int xorAllNums(int *nums1, int nums1Size, int *nums2, int nums2Size) { + int x = 0, y = 0; + for (int i = 0; i < nums1Size; i++) + x ^= nums1[i]; + for (int i = 0; i < nums2Size; i++) + y ^= nums2[i]; + return (nums1Size % 2 * y) ^ (nums2Size % 2 * x); +} + +int main() { + int n11[] = {2, 1, 3}, n21[] = {10, 2, 5, 0}; + int n12[] = {1, 2}, n22[] = {3, 4}; + printf("%d\n", + xorAllNums(n11, ARRAY_SIZE(n11), n21, ARRAY_SIZE(n21))); // expect: 13 + printf("%d\n", + xorAllNums(n12, ARRAY_SIZE(n12), n22, ARRAY_SIZE(n22))); // expect: 0 +} diff --git a/bitwise_xor_pairings.py b/bitwise_xor_pairings.py new file mode 100644 index 0000000..bc1b917 --- /dev/null +++ b/bitwise_xor_pairings.py @@ -0,0 +1,29 @@ +# 2425. Bitwise XOR of All Pairings +from functools import reduce +from operator import xor + +""" +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' +""" + + +class Solution(object): + def xorAllNums(self, nums1, nums2): + """ + :type nums1: List[int] + :type nums2: List[int] + :rtype: int + """ + return (len(nums1) % 2 * reduce(xor, nums2)) ^ ( + len(nums2) % 2 * reduce(xor, nums1) + ) + + +if __name__ == "__main__": + obj = Solution() + print(obj.xorAllNums(nums1=[2, 1, 3], nums2=[10, 2, 5, 0])) + print(obj.xorAllNums(nums1=[1, 2], nums2=[3, 4])) |