Range Query on array whose each element is XOR of index value and previous element
Last Updated : 20 Apr, 2023
Consider an arr[] which can be defined as:

You are given Q queries of the form [l, r]. The task is to output the value of arr[l] ? arr[l+1] ? ..... ? arr[r-1] ? arr[r] for each query.
Examples :
Input : q = 3 q1 = { 2, 4 } q2 = { 2, 8 } q3 = { 5, 9 } Output : 7 9 15 The beginning of the array with constraint look like: arr[] = { 0, 1, 3, 0, 4, 1, 7, 0, 8, 1, 11, .... } For q1, 3 ? 0 ? 4 = 7 For q2, 3 ? 0 ? 4 ? 1 ? 7 ? 0 ? 8 = 9 For q3, 1 ? 7 ? 0 ? 8 ? 1 = 15
Let's observe arr[]
arr[0] = 0 arr[1] = 1 arr[2] = 1 ? 2 arr[3] = 1 ? 2 ? 3 arr[4] = 1 ? 2 ? 3 ? 4 arr[5] = 1 ? 2 ? 3 ? 4 ? 5 ....
Let's make another array, say brr[], where brr[i] = arr[0] ? arr[1] ? arr[2] ? ..... ? arr[i].
brr[i] = arr[0] ? arr[1] ? arr[2] ? ... ? arr[i-1] ? arr[i] = brr[j] ? arr[j+1] ? arr[j+2] ? ..... ? arr[i], for any 0 <= j <= i.
So, arr[l] ? arr[l+1] ? ..... ? arr[r] = brr[l-1] ? brr[r].
Now, let's observe brr[]:
brr[1] = 1 brr[2] = 2 brr[3] = 1 ? 3 brr[4] = 2 ? 4 brr[5] = 1 ? 3 ? 5 brr[6] = 2 ? 4 ? 6 brr[7] = 1 ? 3 ? 5 ? 7 brr[8] = 2 ? 4 ? 6 ? 8
It's easy to observe that in odd indexes brr[i] = 1 ? 3 ? 5 ? .... ? i and for even indexes brr[i] = 2 ? 4 ? 6 ? .... ? i.
For even indexes there are numbers from 1 to i/2 multipliedby 2, that means bits are moved to left by 1, so, brr[i] = 2 ? 4 ? 6 ? .... ? i = (1 ? 2 ? 3 ? ..... ? i/2) * 2.
And for odd indexes there are numbers from 0 to (i - 1)/2 multiplied by 2 and plus 1. That means bits are moved to left by 1, and last bit is made 1. So, brr[i] = 1 ? 3 ? 5 ? .... ? i = (0 ? 1 ? 2 ? .... ? (i - 1)/2) * 2 + x.
x is 1 ? 1 ? 1 ? ..... ? 1 "ones" are repeated (i - 1)/2 + 1 times. So, if (i-1)/2 + 1 is odd then x = 1 else x = 0.
Now, calculation of 1 ? 2 ? 3 ? .... ? x.
Let's prove that (4K) ? (4K + 1) ? (4K + 2) ? (4K + 3) = 0 for 0 <= k.
bitmask(K)00=4K xorsum bitmask(K)01=4K+1 bitmask(K)10=4K+2 bitmask(K)11=4k+3 --------------------- 000000000000=0
So as 0 ? Y = Y then 1 ? 2 ? 3 ? ... ? x = (floor(x/4) x 4) ? ... ? x here are maximum 3 numbers so we can calculate in O(1).
Below is the implementation of this approach:
C++ // CPP Program to solve range query on array // whose each element is XOR of index value // and previous element. #include <bits/stdc++.h> using namespace std; // function return derived formula value. int fun(int x) { int y = (x / 4) * 4; // finding xor value of range [y...x] int ans = 0; for (int i = y; i <= x; i++) ans ^= i; return ans; } // function to solve query for l and r. int query(int x) { // if l or r is 0. if (x == 0) return 0; int k = (x + 1) / 2; // finding x is divisible by 2 or not. return (x %= 2) ? 2 * fun(k) : ((fun(k - 1) * 2) ^ (k & 1)); } void allQueries(int q, int l[], int r[]) { for (int i = 0; i < q; i++) cout << (query(r[i]) ^ query(l[i] - 1)) << endl; } // Driven Program int main() { int q = 3; int l[] = { 2, 2, 5 }; int r[] = { 4, 8, 9 }; allQueries(q, l, r); return 0; }
Java // Java Program to solve range query on array // whose each element is XOR of index value // and previous element. import java.io.*; class GFG { // function return derived formula value. static int fun(int x) { int y = (x / 4) * 4; // finding xor value of range [y...x] int ans = 0; for (int i = y; i <= x; i++) ans ^= i; return ans; } // function to solve query for l and r. static int query(int x) { // if l or r is 0. if (x == 0) return 0; int k = (x + 1) / 2; // finding x is divisible by 2 or not. return ((x %= 2) != 0) ? 2 * fun(k) : ((fun(k - 1) * 2) ^ (k & 1)); } static void allQueries(int q, int l[], int r[]) { for (int i = 0; i < q; i++) System.out.println((query(r[i]) ^ query(l[i] - 1))) ; } // Driven Program public static void main (String[] args) { int q = 3; int []l = { 2, 2, 5 }; int []r = { 4, 8, 9 }; allQueries(q, l, r); } } // This code is contributed by vt_m.
Python3 # Python3 Program to solve range query # on array whose each element is XOR of # index value and previous element. # function return derived formula value. def fun(x): y = (x // 4) * 4 # finding xor value of range [y...x] ans = 0 for i in range(y, x + 1): ans ^= i return ans # function to solve query for l and r. def query(x): # if l or r is 0. if (x == 0): return 0 k = (x + 1) // 2 # finding x is divisible by 2 or not. if x % 2 == 0: return((fun(k - 1) * 2) ^ (k & 1)) else: return(2 * fun(k)) def allQueries(q, l, r): for i in range(q): print(query(r[i]) ^ query(l[i] - 1)) # Driver Code q = 3 l = [ 2, 2, 5 ] r = [ 4, 8, 9 ] allQueries(q, l, r) # This code is contributed # by sahishelangia
C# // C# Program to solve range query on array // whose each element is XOR of index value // and previous element. using System; class GFG { // function return derived formula value. static int fun(int x) { int y = (x / 4) * 4; // finding xor value of range [y...x] int ans = 0; for (int i = y; i <= x; i++) ans ^= i; return ans; } // function to solve query for l and r. static int query(int x) { // if l or r is 0. if (x == 0) return 0; int k = (x + 1) / 2; // finding x is divisible by 2 or not. return ((x %= 2)!=0) ? 2 * fun(k) : ((fun(k - 1) * 2) ^ (k & 1)); } static void allQueries(int q, int []l, int []r) { for (int i = 0; i < q; i++) Console.WriteLine((query(r[i]) ^ query(l[i] - 1))) ; } // Driven Program public static void Main () { int q = 3; int []l = { 2, 2, 5 }; int []r = { 4, 8, 9 }; allQueries(q, l, r); } } // This code is contributed by vt_m.
PHP <?php // PHP Program to solve range // query on array whose each // element is XOR of index // value and previous element. // function return derived // formula value. function fun($x) { $y = ((int)($x / 4) * 4); // finding xor value // of range [y...x] $ans = 0; for ($i = $y; $i <= $x; $i++) $ans ^= $i; return $ans; } // function to solve // query for l and r. function query($x) { // if l or r is 0. if ($x == 0) return 0; $k = (int)(($x + 1) / 2); // finding x is divisible // by 2 or not. return ($x %= 2) ? 2 * fun($k) : ((fun($k - 1) * 2) ^ ($k & 1)); } function allQueries($q, $l, $r) { for ($i = 0; $i < $q; $i++) echo (query($r[$i]) ^ query($l[$i] - 1)) , "\n"; } // Driver Code $q = 3; $l = array( 2, 2, 5 ); $r = array ( 4, 8, 9 ); allQueries($q, $l, $r); // This code is contributed by ajit ?>
JavaScript <script> // Javascript Program to solve range query on array // whose each element is XOR of index value // function return derived formula value. function fun(x) { let y = parseInt(x / 4) * 4; // finding xor value of range [y...x] let ans = 0; for (let i = y; i <= x; i++) ans ^= i; return ans; } // function to solve query for l and r. function query(x) { // if l or r is 0. if (x == 0) return 0; let k = parseInt((x + 1) / 2); // finding x is divisible by 2 or not. return (x %= 2) ? 2 * fun(k) : ((fun(k - 1) * 2) ^ (k & 1)); } function allQueries(q, l, r) { for (let i = 0; i < q; i++) document.write((query(r[i]) ^ query(l[i] - 1)) + "<br>"); } // Driven Program let q = 3; let l = [ 2, 2, 5 ]; let r = [ 4, 8, 9 ]; allQueries(q, l, r); </script>
Time Complexity: O(q* log(n)) where q is the number of queries and n is the largest value of r in the queries.
Auxiliary Space: O(1)
Similar Reads
PreComputation Technique on Arrays Precomputation refers to the process of pre-calculating and storing the results of certain computations or data structures(array in this case) in advance, in order to speed up the execution time of a program. This can be useful in situations where the same calculations are needed multiple times, as
15 min read
Queries for the product of first N factorials Given Q[] queries where each query consists of an integer N, the task is to find the product of first N factorials for each of the query. Since the result could be large, compute it modulo 109 + 7.Examples: Input: Q[] = {4, 5} Output: 288 34560 Query 1: 1! * 2! * 3! * 4! = 1 * 2 * 6 * 24 = 288 Query
7 min read
Range sum queries without updates Given an array arr of integers of size n. We need to compute the sum of elements from index i to index j. The queries consisting of i and j index values will be executed multiple times.Examples: Input : arr[] = {1, 2, 3, 4, 5} i = 1, j = 3 i = 2, j = 4Output : 9 12 Input : arr[] = {1, 2, 3, 4, 5} i
6 min read
Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef
13 min read
Count Primes in Ranges Given a 2d array queries[][] of size n, where each query queries[i] contain 2 elements [l, r], your task is to find the count of number of primes in inclusive range [l, r]Examples: Input: queries[][] = [ [1, 10], [5, 10], [11, 20] ]Output: 4 2 4Explanation: For query 1, number of primes in range [1,
12 min read
Check in binary array the number represented by a subarray is odd or even Given an array such that all its terms is either 0 or 1.You need to tell the number represented by a subarray a[l..r] is odd or even Examples : Input : arr = {1, 1, 0, 1} l = 1, r = 3 Output : odd number represented by arr[l...r] is 101 which 5 in decimal form which is odd Input : arr = {1, 1, 1, 1}
4 min read
GCDs of given index ranges in an Array Given an array arr[] of size N and Q queries of type {qs, qe} where qs and qe denote the starting and ending index of the query, the task is to find the GCD of all the numbers in the range. Examples: Input: arr[] = {2, 3, 60, 90, 50};Index Ranges: {1, 3}, {2, 4}, {0, 2}Output: GCDs of given ranges a
14 min read
Mean of range in array Given an array arr[] of n integers and q queries represented by an array queries[][], where queries[i][0] = l and queries[i][1] = r. For each query, the task is to calculate the mean of elements in the range l to r and return its floor value. Examples: Input: arr[] = [3, 7, 2, 8, 5] queries[][] = [[
12 min read
Difference Array | Range update query in O(1) Given an array arr[] and a 2D array opr[][], where each row represents an operation in the form [l, r, v]. For each operation, add v to all elements from index l to r in arr. Return the updated array after applying all operations.Examples : Input: arr[] = [2, 3, 5, 6, 7], opr[][] = [[2, 4, 2], [3, 4
15+ min read
Range sum query using Sparse Table We have an array arr[]. We need to find the sum of all the elements in the range L and R where 0 <= L <= R <= n-1. Consider a situation when there are many range queries. Examples: Input : 3 7 2 5 8 9 query(0, 5) query(3, 5) query(2, 4) Output : 34 22 15Note : array is 0 based indexed and q
8 min read