LeetCode 2425: Bitwise XOR of All Pairings

Problem Description

Explanation

To solve this problem, we can iterate over each pair of elements from nums1 and nums2, calculate their XOR, and keep updating the result by XORing it with the current pair's XOR value. This way, we can obtain the final XOR value of all pairings.

Time Complexity

The time complexity of this approach is O(n * m), where n is the length of nums1 and m is the length of nums2.

Space Complexity

The space complexity is O(1) as we are not using any extra space other than a few variables.

Solutions

class Solution {
    public int xorAllPairings(int[] nums1, int[] nums2) {
        int xorResult = 0;
        for (int num1 : nums1) {
            for (int num2 : nums2) {
                xorResult ^= (num1 ^ num2);
            }
        }
        return xorResult;
    }
}

Loading editor...