XOR Tips

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Was just randomly going through some problems on codechef which involved the XOR

operation and to my surprise there are many overlaps between all these problems
, some of which I am going to share here:
1) x XOR 0 = x
2) x XOR x = 0
3) let s[i]=a[0] XOR a[1] XOR ... XOR a[i]
then a[l] XOR a[l+1] XOR ... XOR a[r] = s[l-1] XOR x[r]
4) Also in one problem, they had to find in a given list of numbers, four number
s whose XOR would yield 0.
O(n^4): go over all possible quadruplets and check the given condition.
O(n^3 * logn):
now,
w XOR x XOR y XOR z= 0
OR
(w XOR x XOR y XOR z) XOR (z) = 0 XOR z
OR
w XOR x XOR y
= z
Using the above equation, just go over all possible triplets and check wether th
eir XOR yields a number which is already a member of the list (latter can be don
e in O(logn) with the help of sets).

You might also like