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.

Compute the Bitwise XOR of all Numbers in a Range using JavaScript

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:

Table of Content

  • Bitwise XOR Property
  • Segment Trees
  • Binary Indexed Trees (Fenwick Trees)
  • Bitwise Manipulation

Similar Reads

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

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

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

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

Contact Us