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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return Xor in a range
// of a binary array
int xorRange(int pre[], int l, int r)
{
// To store the count of 1s
int cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
// If number of ones are even
if (cntOnes % 2 == 0)
return 0;
// If number of ones are odd
else
return 1;
}
// Function to perform the queries
void performQueries(int queries[][2], int q,
int a[], int n)
{
// To store prefix sum
int pre[n];
// pre[i] stores the number of
// 1s from pre[0] to pre[i]
pre[0] = a[0];
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
// Perform queries
for (int i = 0; i < q; i++)
cout << xorRange(pre, queries[i][0],
queries[i][1])
<< endl;
}
// Driver code
int main()
{
int a[] = { 1, 0, 1, 1, 0, 1, 1 };
int n = sizeof(a) / sizeof(a[0]);
// Given queries
int queries[][2] = { { 0, 3 }, { 0, 2 } };
int q = sizeof(queries) / sizeof(queries[0]);
performQueries(queries, q, a, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return Xor in a range
// of a binary array
static int xorRange(int pre[], int l, int r)
{
// To store the count of 1s
int cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
// If number of ones are even
if (cntOnes % 2 == 0)
return 0;
// If number of ones are odd
else
return 1;
}
// Function to perform the queries
static void performQueries(int queries[][], int q,
int a[], int n)
{
// To store prefix sum
int []pre = new int[n];
// pre[i] stores the number of
// 1s from pre[0] to pre[i]
pre[0] = a[0];
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
// Perform queries
for (int i = 0; i < q; i++)
System.out.println(xorRange(pre, queries[i][0],
queries[i][1]));
}
// Driver code
public static void main(String[] args)
{
int a[] = { 1, 0, 1, 1, 0, 1, 1 };
int n = a.length;
// Given queries
int queries[][] = { { 0, 3 }, { 0, 2 } };
int q = queries.length;
performQueries(queries, q, a, n);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the approach
# Function to return Xor in a range
# of a binary array
def xorRange(pre, l, r):
# To store the count of 1s
cntOnes = pre[r]
if (l - 1 >= 0):
cntOnes -= pre[l - 1]
# If number of ones are even
if (cntOnes % 2 == 0):
return 0
# If number of ones are odd
else:
return 1
# Function to perform the queries
def performQueries(queries, q, a, n):
# To store prefix sum
pre = [0 for i in range(n)]
# pre[i] stores the number of
# 1s from pre[0] to pre[i]
pre[0] = a[0]
for i in range(1, n):
pre[i] = pre[i - 1] + a[i]
# Perform queries
for i in range(q):
print(xorRange(pre, queries[i][0],
queries[i][1]))
# Driver code
a = [ 1, 0, 1, 1, 0, 1, 1 ]
n = len(a)
# Given queries
queries = [[ 0, 3 ], [ 0, 2 ]]
q = len(queries)
performQueries(queries, q, a, n)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return Xor in a range
// of a binary array
static int xorRange(int []pre, int l, int r)
{
// To store the count of 1s
int cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
// If number of ones are even
if (cntOnes % 2 == 0)
return 0;
// If number of ones are odd
else
return 1;
}
// Function to perform the queries
static void performQueries(int [,]queries, int q,
int []a, int n)
{
// To store prefix sum
int []pre = new int[n];
// pre[i] stores the number of
// 1s from pre[0] to pre[i]
pre[0] = a[0];
for (int i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
// Perform queries
for (int i = 0; i < q; i++)
Console.WriteLine(xorRange(pre, queries[i, 0],
queries[i, 1]));
}
// Driver code
public static void Main()
{
int []a = { 1, 0, 1, 1, 0, 1, 1 };
int n = a.Length;
// Given queries
int [,]queries = { { 0, 3 }, { 0, 2 } };
int q = queries.Length;
performQueries(queries, q, a, n);
}
}
// This code is contributed
// by Akanksha Rai
JavaScript
<script>
// Javascript implementation of the approach
// Function to return Xor in a range
// of a binary array
function xorRange(pre, l, r)
{
// To store the count of 1s
let cntOnes = pre[r];
if (l - 1 >= 0)
cntOnes -= pre[l - 1];
// If number of ones are even
if (cntOnes % 2 == 0)
return 0;
// If number of ones are odd
else
return 1;
}
// Function to perform the queries
function performQueries(queries, q, a, n)
{
// To store prefix sum
let pre = new Array(n);
// pre[i] stores the number of
// 1s from pre[0] to pre[i]
pre[0] = a[0];
for (let i = 1; i < n; i++)
pre[i] = pre[i - 1] + a[i];
// Perform queries
for (let i = 0; i < q; i++)
document.write(xorRange(pre, queries[i][0],
queries[i][1]) + "<br>");
}
// Driver code
let a = [ 1, 0, 1, 1, 0, 1, 1 ];
let n = a.length;
// Given queries
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
Similar Reads
Bitwise XOR of a Binary array Given a binary array arr[], the task is to calculate the bitwise XOR of all the elements in this array and print it. Examples: Input: arr[] = {â100â, â1001â, â0011â} Output: 1110 0100 XOR 1001 XOR 0011 = 1110 Input: arr[] = {â10â, â11â, â1000001â} Output: 1000000 Approach: Step 1: First find the max
7 min read
Range Update Queries to XOR with 1 in a Binary Array. Given a binary array arr[] of size N. The task is to answer Q queries which can be of any one type from below: Type 1 - 1 l r : Performs bitwise xor operation on all the array elements from l to r with 1. Type 2 - 2 l r : Returns the minimum distance between two elements with value 1 in a subarray [
15+ min read
Program to make a histogram of an array Given an array of integers, print histogram of array values. Examples:Input : 0 11 2 13 5 12 8 11 12 9Output : 13 | x 12 | x x x 11 | x x x x x 10 | x x x x x 9 | x x x x x x 8 | x x x x x x x 7 | x x x x x x x 6 | x x x x x x x 5 | x x x x x x x x 4 | x x x x x x x x 3 | x x x x x x x x 2 | x x x x
6 min read
Binary search in an object Array for the field of an element What is Binary Searching?Binary searching is a type of algorithm that can quickly search through a sorted array of elements. The algorithm works by comparing the search key to the middle element of the array. If the key is larger than the middle element, the algorithm will search the right side of t
8 min read
Bitwise and (or &) of a range Given two non-negative long integers, x and y given x <= y, the task is to find bit-wise and of all integers from x and y, i.e., we need to compute value of x & (x+1) & ... & (y-1) & y.7 Examples: Input : x = 12, y = 15Output : 12 12 & 13 & 14 & 15 = 12 Input : x = 10,
15+ min read