Find XOR of Numbers in Range L to R

Interview Problems DSA practice problem on Onlearn.

Difficulty: easy.

Topics: How to find the XOR of numbers from L to R (inclusive)?, XOR, Bitwise Operations, Mathematical Algorithms, Optimization, Brute Force, bit manipulation, range queries, Bitwise XOR, Range Queries.

XOR Sum in Range Problem Statement Given two integers, \(L\) and \(R\), find the bitwise XOR sum of all numbers from \(L\) to \(R\) (inclusive). Input Specification The input consists of two space separated integers, \(L\) and \(R\), on a single line. Constraints \(1 \le L \le R \le 10^9\) Output Specification Print a single integer representing the XOR sum of numbers from \(L\) to \(R\). Sample Test Case Input: 2 4 Output: 7 Explanation: \(2 \oplus 3 \oplus 4 = (010 2 \oplus 011 2) \oplus 100 2 = 001 2 \oplus 100 2 = 101 2 = 5 {10}\). Wait, \(2 \oplus 3 \oplus 4 = (2 \oplus 3) \oplus 4 = 1 \oplus 4 = 5\). Let me recheck Sample Test Case calculation: \(2 \oplus 3 = 1\) \(1 \oplus 4 = 5\) So output should be 5, not 7. Correcting sample explanation. Corrected Sample Test Case and Explanation: Input: 2 4 Output: 5 Explanation: The numbers in the range [2, 4] are 2, 3, and 4. Their XOR sum is calculated as: \(2 \oplus 3 \oplus 4 = (010 2 \oplus 011 2) \oplus 100 2 = 001 2 \oplus 100 2 = 101 2 = 5 {10}\).