# XOR of a subarray (range of elements)

Given an array arr[] of n integers and some queries. Each query is of the form (L, R), where L and R are indices of the array. Find XOR value of the subarray arr[L…R], i.e., the value which is obtained when all the elements in the range [L, R] are XORed. Assume 0-based indexing and each array element is a 32 bit unsigned integer.

Examples:

Input : arr[] = {2, 5, 1, 6, 1, 2, 5} L = 1, R = 4 Output : 3 The XOR value of arr[1...4] is 3. Input : arr[] = {2, 5, 1, 6, 1, 2, 5} L = 0, R = 6 Output : 6 The XOR value of arr[0...6] is 6.

**Approach: **

A **simple** solution is to traverse the given array from index L to index R for each query. This results in O(n) time complexity to process each query. If there are q queries, then total time required will be O(q*n). For large number of queries and large arrays this solution is not optimal.

An **efficient** solution is to first preprocess the array. Two observations will be helpful:

- It is mentioned in the problem statement that each array element is a 32-bit unsigned number. Hence the result will also be a 32-bit unsigned number.
- When multiple bits are XORed, the result is 1 if there are odd number of 1s, otherwise the result is 0.

Using observation 1, create a two dimensional array cnt of size 32*n. This array will be used to store count of 1s. An element cnt[i][j] represents count of number of 1s for ith bit upto index j, i.e., how many 1s are present in subarray arr[0..j] at ith bit position.

According to observation 2 to obtain XOR value, number of 1s for all 32 bit positions in the subarray arr[L…R] needs to be found. This can be done with the help of cnt array. Number of ones for ith bit position in subarray arr[L…R] is cnt[i][R] – cnt[i][L-1]. If the number of 1s for ith bit are odd, then the ith bit will be set in result. The result can then be obtained by adding power of 2 corresponding to ith bit if it is set.

Processing each query according to this algorithm will take O(32) i.e. constant time. The preprocessing stage in which cnt array is created will take O(n) time. Thus, total time complexity of this solution for q queries is O(n+q).

**Implementation: **

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to preprocess the array and find count of ` `// number of ones upto jth index for ith bit. ` `void` `preprocess(` `int` `arr[], ` `int` `n, vector<vector<` `int` `> >& cnt) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `// Run a loop for each bit position from ` ` ` `// 0 to 32. ` ` ` `for` `(i = 0; i < 32; i++) { ` ` ` `cnt[i][0] = 0; ` ` ` `for` `(j = 0; j < n; j++) { ` ` ` `if` `(j > 0) { ` ` ` ` ` `// store previous count of 1s ` ` ` `// for ith bit position. ` ` ` `cnt[i][j] = cnt[i][j - 1]; ` ` ` `} ` ` ` ` ` `// Check if ith bit for jth element ` ` ` `// of array is set or not. If it is ` ` ` `// set then increase count of 1 for ` ` ` `// ith bit by 1. ` ` ` `if` `(arr[j] & (1 << i)) ` ` ` `cnt[i][j]++; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to find XOR value for a range of array elements. ` `int` `findXOR(` `int` `L, ` `int` `R, ` `const` `vector<vector<` `int` `> > cnt) ` `{ ` ` ` ` ` `// variable to store final answer. ` ` ` `int` `ans = 0; ` ` ` ` ` `// variable to store number of 1s for ith bit ` ` ` `// in the range L to R. ` ` ` `int` `noOfOnes; ` ` ` ` ` `int` `i, j; ` ` ` ` ` `// Find number of 1s for each bit position from 0 ` ` ` `// to 32. ` ` ` `for` `(i = 0; i < 32; i++) { ` ` ` `noOfOnes = cnt[i][R] - ((L > 0) ? cnt[i][L - 1] : 0); ` ` ` ` ` `// If number of 1s are odd then in the result ` ` ` `// ith bit will be set, i.e., 2^i will be present in ` ` ` `// the result. Add 2^i in ans variable. ` ` ` `if` `(noOfOnes & 1) { ` ` ` `ans += (1 << i); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 5, 1, 6, 1, 2, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `vector<vector<` `int` `> > cnt(32, vector<` `int` `>(n)); ` ` ` `preprocess(arr, n, cnt); ` ` ` `int` `L = 1; ` ` ` `int` `R = 4; ` ` ` `cout << findXOR(L, R, cnt); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity: ** O(n+q)

**Auxiliary Space: ** O(n)

## Recommended Posts:

- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum Subarray Sum in a given Range
- Range query for Largest Sum Contiguous Subarray
- Elements to be added so that all elements of a range are present in array
- Longest subarray in which all elements are greater than K
- Smallest subarray such that all elements are greater than K
- Maximum Subarray Sum after inverting at most two elements
- Longest subarray not having more than K distinct elements
- Distinct elements in subarray using Mo's Algorithm
- Longest Subarray having sum of elements atmost 'k'
- Sorting array except elements in a subarray
- Maximum Subarray Sum Excluding Certain Elements
- Length of the longest Subarray with only Even Elements
- Longest subarray with elements having equal modulo K
- Length of the largest subarray with contiguous elements | Set 1

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.