Skip to content

Commit 958c0a1

Browse files
committed
Sync LeetCode submission Runtime - 1 ms (83.81%), Memory - 36.4 MB (61.27%)
1 parent 86771fb commit 958c0a1

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You are given two <strong>0-indexed</strong> arrays, <code>nums1</code> and <code>nums2</code>, consisting of non-negative integers. There exists another array, <code>nums3</code>, which contains the bitwise XOR of <strong>all pairings</strong> of integers between <code>nums1</code> and <code>nums2</code> (every integer in <code>nums1</code> is paired with every integer in <code>nums2</code> <strong>exactly once</strong>).</p>
2+
3+
<p>Return<em> the <strong>bitwise XOR</strong> of all integers in </em><code>nums3</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums1 = [2,1,3], nums2 = [10,2,5,0]
10+
<strong>Output:</strong> 13
11+
<strong>Explanation:</strong>
12+
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
13+
The bitwise XOR of all these numbers is 13, so we return 13.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums1 = [1,2], nums2 = [3,4]
20+
<strong>Output:</strong> 0
21+
<strong>Explanation:</strong>
22+
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
23+
and nums1[1] ^ nums2[1].
24+
Thus, one possible nums3 array is [2,5,1,6].
25+
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
33+
<li><code>0 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li>
34+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Approach 2: Space Optimized Bit Manipulation
2+
3+
# n, m = len(nums1), len(nums2)
4+
# Time: O(n + m)
5+
# Space: O(1)
6+
7+
class Solution:
8+
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
9+
# Initialize XOR results for both arrays
10+
xor1, xor2 = 0, 0
11+
12+
len1, len2 = len(nums1), len(nums2)
13+
14+
# If nums2 length is odd, each element in nums1 appears odd times in final result
15+
if len2 % 2:
16+
for num in nums1:
17+
xor1 ^= num
18+
19+
# If nums1 length is odd, each element in nums2 appears odd times in final result
20+
if len1 % 2:
21+
for num in nums2:
22+
xor2 ^= num
23+
24+
return xor1 ^ xor2

0 commit comments

Comments
 (0)