2425. Bitwise XOR of All Pairings
Difficulty: Medium
Topics: Array, Bit Manipulation, Brainteaser
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.
Example 1:
- Input: nums1 = [2,1,3], nums2 = [10,2,5,0]
- Output: 13
-
Explanation:
- A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
- The bitwise XOR of all these numbers is 13, so we return 13.
Example 2:
- Input: nums1 = [1,2], nums2 = [3,4]
- Output: 0
-
Explanation:
- All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0], and nums1[1] ^ nums2[1].
- Thus, one possible nums3 array is [2,5,1,6].
- 2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
Constraints:
1 <= nums1.length, nums2.length <= 1050 <= nums1[i], nums2[j] <= 109
Hint:
- Think how the count of each individual integer affects the final answer.
- If the length of nums1 is m and the length of nums2 is n, then each number in nums1 is repeated n times and each number in nums2 is repeated m times.
Solution:
We need to think carefully about how the bitwise XOR operates and how the pairing of elements in nums1 and nums2 affects the result.
Key Observations:
- Each element in
nums1pairs with every element innums2, and vice versa. -
In
nums3, each number fromnums1andnums2appears multiple times:- Each number from
nums1appearsntimes (wherenis the length ofnums2). - Each number from
nums2appearsmtimes (wheremis the length ofnums1).
- Each number from
The XOR operation has a property that if a number appears an even number of times, it will cancel out (because
x ^ x = 0), and if it appears an odd number of times, it contributes to the final XOR result.
Approach:
- For each bit position (from 0 to 31, since
nums1[i]andnums2[j]are at most10^9), we will calculate how many times this bit is set (i.e.,1) in the XOR result of all pairings. - Each number in
nums1contributes to the final XORntimes, and each number innums2contributes to the final XORmtimes.
Using these observations, we can reduce the problem to counting the number of times each bit is set (odd number of times) across all pairings.
Plan:
- Count the number of
1s in each bit position fornums1andnums2. - For each bit, check whether the total count of
1s (from bothnums1andnums2) for that bit is odd or even. - If the count is odd, set that bit in the final XOR result.
Let's implement this solution in PHP: 2425. Bitwise XOR of All Pairings
<?php
function xorAllNums($nums1, $nums2) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example 1
$nums1 = [2, 1, 3];
$nums2 = [10, 2, 5, 0];
echo xorAllNums($nums1, $nums2); // Output: 13
// Example 2
$nums1 = [1, 2];
$nums2 = [3, 4];
echo xorAllNums($nums1, $nums2); // Output: 0
?>
Explanation:
-
$mand$n: Store the lengths ofnums1andnums2respectively. -
$result: This variable will store the final XOR result. -
Loop over 32 bits: Since each number is at most
10^9, we only need to consider the first 32 bits. -
Counting bits: For each bit position, we count how many times this bit is set in
nums1andnums2. -
Calculate total counts: Each bit from
nums1contributesntimes to the total count, and each bit fromnums2contributesmtimes. - XOR result update: If the total count for a particular bit is odd, we update the result to set that bit.
Complexity:
-
Time Complexity: O(m + n), where m and n are the lengths of
nums1andnums2, respectively. - Space Complexity: O(1), as we use constant extra space.
Example Walkthrough:
Example 1:
$nums1 = [2, 1, 3];
$nums2 = [10, 2, 5, 0];
echo xorAllPairings($nums1, $nums2); // Output: 13
Example 2:
$nums1 = [1, 2];
$nums2 = [3, 4];
echo xorAllPairings($nums1, $nums2); // Output: 0
This solution is efficient and works within the problem's constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)