Hey, I recently got this problem in an online assessment.
Count number of subarrays such that, xor of first and last element of subarray should be equal to xor of remaining elements in the subarray. And the minimum size of subarray in consideration would be greater than equal to 3.
Thanks
this means the subarray's xor=0
Yes, now that I think the way I descibed, it means exactly what you're saying.
Ig this problem came in one of CodeChef contest
Here's an $$$O(N)$$$ solution:
We want to count subarrays such that:
$$$a[l] \oplus a[r] = a[l+1] \oplus \dots \oplus a[r-1]$$$
$$$0 = a[l] \oplus \dots \oplus a[r]$$$
$$$a[1] \oplus \dots \oplus a[l-1] = a[1] \oplus \dots a[r]$$$
Now, for a fixed $$$r$$$, we just need to count the number of $$$l$$$ such that the equation is true. We can do so in $$$O(1)$$$ via prefix sums stored in a hashmap.
Code:
Amazing man!
I guess you can just count the number of subarrays with 0 xor, by using prefix_xor and some combinatorics