given to integers A and B A<=B find XOR of all the elements between them. Expected Complexity: logN asked 10 Sep '16, 12:36

Let us denote $f(n) = 1 \oplus 2 \oplus 3 \oplus \dots \oplus n$, where $\oplus$ denotes XOR operation Now we can find out easily that, $$ f(n) = \left\{\begin{array}{@{}lr@{}} n, & \text{n mod 4 = 0}\\ 1, & \text{n mod 4 = 1}\\ n+1, & \text{n mod 4 = 2}\\ 0, & \text{n mod 4 = 3} \end{array}\right\} $$ Time Complexity  $O(1)$ answered 10 Sep '16, 16:42

No, it is not always true. If A=2, B=3, then your answer will be 5. But 2^3 = 1. answered 10 Sep '16, 13:52

@jaydeep97 its right. The answer will be f(3)^f(1)=0^1=1. answered 02 Oct '16, 19:52
