![]() |
VOOZH | about |
Given two integers L and R, the task is to find the XOR of elements of the range [L, R].
Examples:
Input: L = 4, R = 8
Output: 8
Explanation: 4 ^ 5 ^ 6 ^ 7 ^ 8 = 8Input: L = 2, R = 4
Output: 5
Explanation: 2 ^ 3 ^ 4 = 5
Table of Content
Initialize answer as zero, Traverse all numbers from L to R and perform XOR of the numbers one by one with the answer.
8
Use the repeating XOR pattern from
1 to nand compute the range XOR as: XOR(L to R) = XOR(1 to R) ^ XOR(1 to L-1).
How does this work? We mainly get the elements from 1 to L-1 appeared twice in x1 ^ x2 and if we do XOR of an element with itself, we get 0 and if we do XOR of 0 with an element x, we get x.
How do we find XOR of 1 to n Find the remainder of n by moduling it with 4.
Please refer XOR of 1 to n for working of this.
8