The main logic behind problems involving XOR operation is that you have to update the bits of final answer.
- XOR = 0 if A=B else XOR = 1, so 1^1^0^0=0, you have to find whether 1 is present even number of times or not,
- To check the above point, instead of doing XOR operation at each iteration, we can update the bits of final answer. I would like to explain this point with an example
lets take A= [ 4, 5, 7, 9], so we need to find 4^5^7^9
4= 0100
5= 0101
7= 0111
9= 1001
ans= 1313
to find the bits at each position of answer we will simply add the corresponding bits of inputs and take mod of each value with 2, so 1313=1111 = 15 in decimal
Thus 4^5^7^9=15. This concept to find final answer involving XOR operations is applicable on a wide range of questions.
Now coming to this question, we have to know which number will occur how many times in all the sub-arrays
answer: element at ith index will occur (i+1)*(n-i) times in total, n=size of array.
I hope this was helpful to you.
Happy coding.