Sensible and Simple c++


#1
int Solution::solve(vector<int> &A) {
    int res=0;
    for(int i=0;i<A.size();i++)
        {
            int cnt = (i+1)*(A.size()-i);// total number of occurences of A[i]'s in all xors
            if(cnt %2!=0)
                res ^=A[i];
        }
    return res;
}

#2

hey ,your solution is brilliant?
but i have one doubt…how to calculate count of each element in all subarrays?


#3

@nandini-raj
Select an index i
[0…i-1. i ] [ i+1… …n-1]
It will divide the array into two halves where first contains i+1 elements and second with (n-i) elements;
now you can construct any sub-array including i by selecting proper indices from the first part and then the second part.
Now since we can select any indices on the left part and we will have (n-i) indices for each of them on the second part thus we will end up with (i+1)*(n-i) total combinations i.e. the total number of possible subarrays including A[i] .

Hope that makes sense!