GeeksforGeeks Easy

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).

Constraints
  • 1 <= l <= r <= 10⁹
bit-manipulationxormathsprefix
Open on GeeksforGeeks ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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).

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).

2

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.
3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findXOR(self, l, r):
▶3 def upto(n):
▶4 rem = n % 4
▶5 if rem == 0:
▶6 return n
▶7 if rem == 1:
▶8 return 1
▶9 if rem == 2:
▶10 return n + 1
▶11 return 0
▶12 
▶13 return upto(r) ^ upto(l - 1)
lr000001000120010300114010050101601107011181000rule by remainder of n ÷ 40: n1: 12: n + 13: 0combine the two prefixesf(8)f(3)answerxor=XOR every number from 4 to 8
l4
r8
Looping over the range works here, but with r up to 10⁹ it is far too slow. Instead, write the answer as the XOR of 0 to 8, with the XOR of 0 to 3 cancelled out of it.
lr000001000120010300114010050101601107011181000xor = 0rule by remainder of n ÷ 40: n1: 12: n + 13: 0combine the two prefixesf(8)f(3)answerxor=0 to 3 xor to 0
n8
block0 to 3
Read the bits under 0 to 3: the higher bits are the same in all four and cancel, and the last two bits are 00, 01, 10, 11, which also cancel. So the whole block contributes 0.
lr000001000120010300114010050101601107011181000xor = 0xor = 0rule by remainder of n ÷ 40: n1: 12: n + 13: 0combine the two prefixesf(8)f(3)answerxor=4 to 7 xor to 0
n8
block4 to 7
Read the bits under 4 to 7: the higher bits are the same in all four and cancel, and the last two bits are 00, 01, 10, 11, which also cancel. So the whole block contributes 0.
lr000001000120010300114010050101601107011181000xor = 0xor = 0rule by remainder of n ÷ 40: n1: 12: n + 13: 0combine the two prefixes8f(8)f(3)answerxor=left over: 8 → XOR of 0 to 8 is 8
n % 40
f(8)8
8 leaves remainder 0, so the numbers after the last full block are 8. Their XOR is 8, which is what the rule gives.
lr000001000120010300114010050101601107011181000xor = 0rule by remainder of n ÷ 40: n1: 12: n + 13: 0combine the two prefixes8f(8)0f(3)answerxor=XOR of 0 to 3 is 0 (remainder 3)
n % 43
f(3)0
Now the prefix to cancel: 0 to 3. It ends on a full block, so it is 0.
lr000001000120010300114010050101601107011181000rule by remainder of n ÷ 40: n1: 12: n + 13: 0combine the two prefixes8f(8)0f(3)8answerxor=answer: 8 xor 0 = 8
f(r) xor f(l − 1)8 xor 0
answer8
The numbers 0 to 3 sit in both prefixes, so XOR removes them and only 4 to 8 remains. Two remainder lookups and one XOR, whatever the size of the range.
05

Common pitfalls

Cancelling f(l) instead of f(l − 1)

✗ Wrong
return upto(r) ^ upto(l)
✓ Right
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

✗ Wrong
if rem == 2:
    return 0
return n + 1
✓ Right
if rem == 2:
    return n + 1
return 0

f(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.

06

Edge cases

l = 1

l − 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.

07

Complexity

Time
O(1)
Space
O(1)
Two remainder checks and one XOR, however far apart l and r are. A loop from l to r would take up to 10⁹ steps.
08

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.