Posted on

Description

Submission

class Solution {
public:
    int countTriplets(vector<int>& arr) {
        if(arr.size() < 2) return 0;

        int n = arr.size();
        arr.insert(arr.begin(), 0);
        vector<int> prexor(n+1);
        prexor[0] = 0;

        for(int i = 1; i <=n; ++i) {
            prexor[i] = prexor[i-1] ^ arr[i];
        }

        int ret = 0;
        for(int i = 0; i < n; ++i) {
            for(int k = i + 1; k < n+1; ++k) {
                if((prexor[i] ^ prexor[k]) == 0) {
                    ret += (k - i - 1);
                }
            }
        }
        return ret;
    }
};

Leave a Reply

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