XOR of Numbers in a Given Range
XOR of Numbers in a Given Range is a GFG problem (Easy). Given two integers l and r, return the XOR of every integer from l to r, that is l ^ (l + 1) ^ … ^ r.
Both ends can be as large as 10⁹, so a loop over the range may take a billion steps. The answer has to come from a formula, in O(1).
- 1 <= l <= r <= 10⁹
Intuition
XOR undoes itself: x ^ x = 0. So, like a range sum, the XOR from l to r is a difference of two prefixes, f(r) ^ f(l − 1), where f(n) is the XOR of 0 to n. Every number below l sits in both prefixes and cancels.
That leaves f(n). Split 0 to n into blocks of four that start at multiples of 4. In 4k to 4k + 3 the higher bits are equal in all four numbers and cancel, and the last two bits run 00, 01, 10, 11, which cancel too. Every full block is 0, so f(n) depends only on the numbers after the last full block, and n % 4 says which they are.
A range question over consecutive integers that uses XOR is a prefix-difference problem, the same shape as range sums, because XOR is its own inverse.
Approach
Before reading on, write out the XOR of 0 to n for n = 0, 1, …, 7 and look for a cycle. Then decide which prefix to cancel so that l itself stays in. Aim for O(1).
Turn the range into two prefixes
Let f(n) be the XOR of 0 to n. The numbers 0 to r are the numbers 0 to l − 1 followed by l to r, so f(r) = f(l − 1) ^ answer. XOR both sides with f(l − 1): it cancels on the right, leaving answer = f(r) ^ f(l − 1).
Read f(n) off n % 4
Only the numbers after the last full block of four matter:
- remainder 0: just n is left, so
f(n) = n; - remainder 1: n − 1 and n differ only in the last bit, so 1;
- remainder 2: that 1 XOR n gives
n + 1; - remainder 3: n closes a block, so 0.
Combine the two lookups
Return upto(r) ^ upto(l - 1). Each lookup is one remainder and one branch, so the cost is the same whether the range holds 1 number or a billion.
Code in Python, C++ and Java
Common pitfalls
Cancelling f(l) instead of f(l − 1)
return upto(r) ^ upto(l)
return upto(r) ^ upto(l - 1)
f(l) includes l itself, so cancelling it removes l from the answer too. For l = 3, r = 3 the answer is 3, but f(3) ^ f(3) gives 0.
Swapping the results for remainders 2 and 3
if rem == 2:
return 0
return n + 1if rem == 2:
return n + 1
return 0f(2) = 0 ^ 1 ^ 2 = 3, which is n + 1, and f(3) = 3 ^ 3 = 0. With the two swapped, l = 1, r = 2 returns 0 instead of 1 ^ 2 = 3. Checking f(0) to f(7) against a plain loop catches it.
Edge cases
l = 1l − 1 = 0 and f(0) = 0, so the answer is f(r) on its own. The remainder-0 branch returns n = 0, so no special case is needed.
Complexity
l and r are. A loop from l to r would take up to 10⁹ steps.XOR of Numbers in a Given Range FAQ
What is the XOR of 1 to n?
The same as the XOR of 0 to n, since XOR with 0 changes nothing. So it is f(n): n, 1, n + 1 or 0 when n % 4 is 0, 1, 2 or 3. For n = 6 that gives 7, and indeed 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 7.
How is this different from Bitwise AND of Numbers Range?
LeetCode 201 ANDs every number from left to right. AND has no way to undo a value, so prefixes cannot be cancelled there; the answer is instead the common binary prefix of left and right. XOR cancels itself, which is exactly what makes f(r) ^ f(l − 1) work.
Does the prefix XOR trick work on an array?
Yes, but without the cycle of 4, because array values follow no pattern. Store p[i], the XOR of the first i values, in one pass. The XOR of positions l to r is then p[r + 1] ^ p[l], which answers each query in O(1) after O(n) setup.