Compute the Bitwise XOR of all Numbers in a Range using JavaScript
Last Updated :
02 May, 2024
The complexity of bitwise XOR computation for all the numbers in a specific range is the most frequent difficulty that many people face during the problem-solving process.
Problem Description: Given two integers, L and R, find the bitwise XOR of all numbers in the inclusive range from L to R. There are several approaches to computing the bitwise XOR of all numbers in a range using JavaScript which are as follows:
Bitwise XOR Property
One optimized approach leverages the XOR operation's properties. XORing a number with itself results in zero (a⊕a=0), and XORing zero with any number gives the same number (a⊕0=a). By exploiting these properties, we can XOR consecutive numbers to cancel each other out, leaving only the XOR of the first and last numbers in the range. This method achieves a time complexity of O(1) by finding the XOR of the first L−1 numbers and the first R numbers, then XORing the results to obtain the final answer.
Example: Let's us consider an example where L=3 and R=6.
The XOR of all numbers from 3 to 6 is calculated as follows:
3⊕4⊕5⊕6=(3⊕4)⊕(5⊕6)
=(7)⊕(3)
=4
Example: To demonstrate computing the bitwise XOR of all numbers in a range using bitwise XOR properties in JavaScript.
JavaScript
function xorRange(L, R) {
// XOR of numbers from 1 to n
const xorN = (n) => {
if (n % 4 === 0) return n;
if (n % 4 === 1) return 1;
if (n % 4 === 2) return n + 1;
return 0;
};
// XOR of numbers from 1 to L-1
const xorLMinusOne = xorN(L - 1);
// XOR of numbers from 1 to R
const xorR = xorN(R);
// XOR of numbers from L to R
const xorLR = xorLMinusOne ^ xorR;
return xorLR;
}
const L = 3;
const R = 6;
const result = xorRange(L, R);
console.log(`Bitwise XOR of all numbers from ${L} to ${R} is ${result}`);
Output:
Bitwise XOR of all numbers from 3 to 6 is 4
Time Complexity: O(1), as it does not depend on the size of the range.
Space Complexity: O(1), as we only use a constant amount of additional space regardless of the input size.
Segment Trees
Segment trees are versatile data structures used for storing and querying information about intervals or segments efficiently. We can build a segment tree for the given range and then use it to query the XOR of numbers in the range.
Example: Let us consider an example where we have L=5 and R=15. We will build a segment tree for the range [5, 15] and then query it to find the XOR of numbers from 5 to 15.
JavaScript
// Segment Tree
function updateSegmentTree(tree, index, val, node, start, end) {
if (start === end) {
tree[node] ^= val;
return;
}
let mid = Math.floor((start + end) / 2);
if (index <= mid) {
updateSegmentTree(tree, index, val, 2 * node, start, mid);
} else {
updateSegmentTree(tree, index, val, 2 * node + 1, mid + 1, end);
}
tree[node] = tree[2 * node] ^ tree[2 * node + 1];
}
// Query Segment Tree
function querySegmentTree(tree, left, right, node, start, end) {
if (right < start || left > end) {
return 0;
}
if (left <= start && right >= end) {
return tree[node];
}
let mid = Math.floor((start + end) / 2);
let leftXor = querySegmentTree(tree, left, right, 2 * node, start, mid);
let rightXor = querySegmentTree(tree, left, right, 2 * node + 1, mid + 1, end);
return leftXor ^ rightXor;
}
function xorRange(L, R) {
// Build Segment Tree
const n = R - L + 1;
const tree = new Array(4 * n).fill(0);
for (let i = L; i <= R; i++) {
updateSegmentTree(tree, i - L + 1, i, 1, 1, n);
}
// Query Segment Tree
return querySegmentTree(tree, 1, n, 1, 1, n);
}
const L = 5;
const R = 15;
const result = xorRange(L, R);
console.log(`Bitwise XOR of all numbers from ${L} to ${R} is ${result}`);
Output:
Bitwise XOR of all numbers from 5 to 15 is 4
Time Complexity: O(N) , where N is the number of elements in the array. Querying the segment tree takes O(log N) time.
Space Complexity: O(N) for storing the tree structure.
Binary Indexed Trees (Fenwick Trees)
Binary Indexed Trees, also known as Fenwick Trees, are efficient data structures used for querying and updating cumulative frequency or prefix sum arrays. We can modify the Fenwick Tree to support bitwise XOR operations and use it to compute the XOR of numbers in the given range efficiently.
Example: Consider an example where L=3 and R=7. We construct a Fenwick Tree for the given range and then query it to find the XOR of numbers from 3 to 7.
JavaScript
// Update Binary Indexed Tree (Fenwick Tree)
function updateBIT(BIT, index, val) {
while (index < BIT.length) {
BIT[index] ^= val;
index += index & -index;
}
}
// Query Binary Indexed Tree (Fenwick Tree)
function queryBIT(BIT, index) {
let xor = 0;
while (index > 0) {
xor ^= BIT[index];
index -= index & -index;
}
return xor;
}
function xorRange(L, R) {
// Build Fenwick Tree
const BIT = new Array(R + 1).fill(0);
for (let i = L; i <= R; i++) {
updateBIT(BIT, i, i);
}
// Query Fenwick Tree
const xorL = queryBIT(BIT, L - 1);
const xorR = queryBIT(BIT, R);
return xorL ^ xorR;
}
const L = 3;
const R = 6;
const result = xorRange(L, R);
console.log(`Bitwise XOR of all numbers from ${L} to ${R} is ${result}`);
Output:
Bitwise XOR of all numbers from 3 to 7 is 3
Time Complexity: O(NlogN) time, where N is the size of the range. Querying the Fenwick Tree takes O(logN) time.
Space Complexity: O(N) for storing the tree structure.
Bitwise Manipulation
In this approach, we directly manipulate bits to compute the XOR of numbers in the given range without using any additional data structures. This approach is based on observing patterns in the XOR operation.
Example: Consider an example where L=4 and R=8. We compute the XOR of all numbers from 4 to 8 by observing patterns in the XOR operation.
JavaScript
function xorRange(L, R) {
let xorLR = 0;
for (let i = L; i <= R; i++) {
xorLR ^= i;
}
return xorLR;
}
const L = 4;
const R = 8;
const result = xorRange(L, R);
console.log(`Bitwise XOR of all numbers from ${L} to ${R} is ${result}`);
Output:
Bitwise XOR of all numbers from 4 to 8 is 8
Time Complexity: O(N), where N is the size of the range.
Space Complexity: O(1)
Similar Reads
Count Number of Bits to be Flipped to Convert A to B using JavaScript
Given two numbers, A and B. Write a program to count the number of bits needed to be flipped to obtain B from A. Examples: Input: A = 10, B = 20Output: 4Explanation: Binary representation of A is 00001010Binary representation of B is 00010100We need to flip highlighted four bits in A to obtain B fro
3 min read
How to set, clear and toggle a single bit of a number in JavaScript?
Given a JavaScript number and the task is to set, clear and toggle a single bit of any number. Here are few approaches discussed with the help of JavaScript. Setting the bit: Get the position of the bit to be set(starts from right, first bit is at 0 position), Ex. (bitSet = 3) Get the mask by mask =
3 min read
Sum of Squares of Even Numbers in an Array using JavaScript
JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array
2 min read
Print all Odd Numbers in a Range in JavaScript Array
Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various a
4 min read
Toggle Bits Given Range using JavaScript
Given an integer n and a range [l, r], our task is to toggle the bits of n from the lth to rth position (1-based index) using JavaScript. Toggling a bit means changing 1 to 0 and 0 to 1. Examples: Input: n = 29, l = 2, r = 4Output: 19Explanation: (29)10 = (11101)2 (19)10 = (10011)2The bits in the ra
3 min read
How to calculate the XOR of array elements using JavaScript ?
In this article, we are given an array, and the task is to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below: Method 1: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of
2 min read
Print all Even Numbers in a Range in JavaScript Array
We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read
Print all Positive Numbers in a Range in JavaScript Array
To print all the possible numbers in a range from the JavaScript array, we need to iterate over the array and apply the condition to filter out the positive numbers. Once the elements are extracted, we can print them in the console. Example:Input: arr = [-22, 45, 63, -88, -69]Range = [1 to 70]Output
3 min read
Print Strong Numbers Within a Range using JavaScript
In mathematics, a strong number is a number whose sum of the factorial of its digits is equal to the number itself. The task is to print all strong numbers within a given range in JavaScript. Example: 145 is a Strong number because: 1: 14: 4*3*2*1=245: 5*4*3*2*1=120120+24+1=145Since the sum is equal
2 min read
Check if the ith Bit is Set or Not using JavaScript
Checking if the ith bit is set or not in a binary representation is a common task in programming, especially in scenarios where bitwise operations are utilized. In this article, we'll explore multiple approaches to accomplish this using JavaScript. Given a non-negative integer num and an index i, we
2 min read