Posted on

Description

Submission

class Solution {
public:
    int getXORSum(vector<int>& arr1, vector<int>& arr2) {
        int xorsum = 0;

        vector<int> ones(32);
        for(auto x: arr2) {
            for(int i = 0; i < 32; ++i) {
                if((x>>i)&1) ones[i]++;
            }
        }

        int ret = 0;
        for(auto a: arr1) {
            int ans = 0;
            for(int i = 0; i < 32; ++i) {
                if((a>>i)&1) {
                    if(ones[i]%2) ans += (1<<i);
                }
                else continue;
            }
            ret ^= ans;
        }

        return ret;
    }
};

// a,b,c.   d,e,f
// a&d ^ a&e ^ a&f
// b&d ^ b&e ^ b&f



// for i-th bit
// if arr[i] == 0: the whole row = 0
// if arr[i] != 0: the whole row = d[i] ^ e[i] ^ f[i], the xor of arr2(calculate in advance)
// * each bit is independent

Leave a Reply

Your email address will not be published. Required fields are marked *