# XOR in a range of a binary array

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 ^ arr ^ arr ^ arr = 1 ^ 0 ^ 1 ^ 1 = 1
Query 1: arr ^ arr ^ arr = 1 ^ 0 ^ 1 = 0

Input: arr[] = {1, 0, 1, 1, 0, 1, 1}, q[][] = {{1, 1}}
Output: 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 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 ` `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[], ``int` `q, ` `                    ``int` `a[], ``int` `n) ` `{ ` `    ``// To store prefix sum ` `    ``int` `pre[n]; ` ` `  `    ``// pre[i] stores the number of ` `    ``// 1s from pre to pre[i] ` `    ``pre = a; ` `    ``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], ` `                         ``queries[i]) ` `             ``<< endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 0, 1, 1, 0, 1, 1 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``// Given queries ` `    ``int` `queries[] = { { 0, 3 }, { 0, 2 } }; ` `    ``int` `q = ``sizeof``(queries) / ``sizeof``(queries); ` ` `  `    ``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 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 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 to pre[i] ` `    ``pre = a; ` `    ``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 `

Output:

```1
0
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.