XOR in a range of a binary array
Last Updated :
12 Dec, 2022
Given a binary array arr[] of size N and some queries. Each query represents an index range [l, r]. The task is to find the xor of the elements in the given index range for each query i.e. arr[l] ^ arr[l + 1] ^ … ^ arr[r].
Examples:
Input: arr[] = {1, 0, 1, 1, 0, 1, 1}, q[][] = {{0, 3}, {0, 2}}
Output:
1
0
Query 1: arr[0] ^ arr[1] ^ arr[2] ^ arr[3] = 1 ^ 0 ^ 1 ^ 1 = 1
Query 1: arr[0] ^ arr[1] ^ arr[2] = 1 ^ 0 ^ 1 = 0
Input: arr[] = {1, 0, 1, 1, 0, 1, 1}, q[][] = {{1, 1}}
Output: 0
Approach: The main observation is that the required answer will always be either 0 or 1. If the number of 1’s in the given range are odd then the answer will be 1. Otherwise 0. To answer multiple queries in constant time, use a prefix sum array pre[] where pre[i] stores the number of 1’s in the original array in the index range [0, i] which can be used to find the number of 1’s in any index range of the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int xorRange( int pre[], int l, int r)
{
int cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
if (cntOnes % 2 == 0)
return 0;
else
return 1;
}
void performQueries( int queries[][2], int q,
int a[], int n)
{
int pre[n];
pre[0] = a[0];
for ( int i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
for ( int i = 0; i < q; i++)
cout << xorRange(pre, queries[i][0],
queries[i][1])
<< endl;
}
int main()
{
int a[] = { 1, 0, 1, 1, 0, 1, 1 };
int n = sizeof (a) / sizeof (a[0]);
int queries[][2] = { { 0, 3 }, { 0, 2 } };
int q = sizeof (queries) / sizeof (queries[0]);
performQueries(queries, q, a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int xorRange( int pre[], int l, int r)
{
int cntOnes = pre[r];
if (l - 1 >= 0 )
cntOnes -= pre[l - 1 ];
if (cntOnes % 2 == 0 )
return 0 ;
else
return 1 ;
}
static void performQueries( int queries[][], int q,
int a[], int n)
{
int []pre = new int [n];
pre[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
pre[i] = pre[i - 1 ] + a[i];
for ( int i = 0 ; i < q; i++)
System.out.println(xorRange(pre, queries[i][ 0 ],
queries[i][ 1 ]));
}
public static void main(String[] args)
{
int a[] = { 1 , 0 , 1 , 1 , 0 , 1 , 1 };
int n = a.length;
int queries[][] = { { 0 , 3 }, { 0 , 2 } };
int q = queries.length;
performQueries(queries, q, a, n);
}
}
|
Python3
def xorRange(pre, l, r):
cntOnes = pre[r]
if (l - 1 > = 0 ):
cntOnes - = pre[l - 1 ]
if (cntOnes % 2 = = 0 ):
return 0
else :
return 1
def performQueries(queries, q, a, n):
pre = [ 0 for i in range (n)]
pre[ 0 ] = a[ 0 ]
for i in range ( 1 , n):
pre[i] = pre[i - 1 ] + a[i]
for i in range (q):
print (xorRange(pre, queries[i][ 0 ],
queries[i][ 1 ]))
a = [ 1 , 0 , 1 , 1 , 0 , 1 , 1 ]
n = len (a)
queries = [[ 0 , 3 ], [ 0 , 2 ]]
q = len (queries)
performQueries(queries, q, a, n)
|
C#
using System;
class GFG
{
static int xorRange( int []pre, int l, int r)
{
int cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
if (cntOnes % 2 == 0)
return 0;
else
return 1;
}
static void performQueries( int [,]queries, int q,
int []a, int n)
{
int []pre = new int [n];
pre[0] = a[0];
for ( int i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
for ( int i = 0; i < q; i++)
Console.WriteLine(xorRange(pre, queries[i, 0],
queries[i, 1]));
}
public static void Main()
{
int []a = { 1, 0, 1, 1, 0, 1, 1 };
int n = a.Length;
int [,]queries = { { 0, 3 }, { 0, 2 } };
int q = queries.Length;
performQueries(queries, q, a, n);
}
}
|
Javascript
<script>
function xorRange(pre, l, r)
{
let cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
if (cntOnes % 2 == 0)
return 0;
else
return 1;
}
function performQueries(queries, q, a, n)
{
let pre = new Array(n);
pre[0] = a[0];
for (let i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
for (let i = 0; i < q; i++)
document.write(xorRange(pre, queries[i][0],
queries[i][1]) + "<br>" );
}
let a = [ 1, 0, 1, 1, 0, 1, 1 ];
let n = a.length;
let queries = [ [ 0, 3 ], [ 0, 2 ] ];
let q = queries.length;
performQueries(queries, q, a, n);
</script>
|
Time Complexity: O(n + q), where n is the size of the given array and q is the number of queries given.
Auxiliary Space: O(n), where n is the size of the given array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...