# Count subarrays with sum equal to its XOR value

Given an array **arr[]** containing **N** elements, the task is to count the number of sub-arrays whose XOR of all the elements is equal to the sum of all the elements in the subarray.

**Examples:**

Input:arr[] = {2, 5, 4, 6}

Output:5

Explanation:

All the subarrays {{2}, {5}, {4}, {6}} satisfies the above condition since the XOR of the subarrays is same as the sum. Apart from these, the subarray {2, 5} also satisfies the condition:

(2 xor 5) = 7 = (2 + 5)

Input:arr[] = {1, 2, 3, 4, 5}

Output:7

**Naive Approach:** The naive approach for this problem is to consider all the sub-arrays and for every subarray, check if the XOR is equal to the sum.

**Time Complexity:** O(N^{2})

**Efficient Approach:** The idea is to use the concept of sliding window. First, we calculate the window for which the above condition is satisfied and then we slide through every element till N. The following steps can be followed to compute the answer:

- Maintain two pointers
*left*and*right*initially assigned to zero. - Calculate the window using right pointer where the condition A xor B = A + B is satisfied.
- Count of the sub-arrays will be
**right – left**. - Iterate through every element and remove the previous element.

Below is the implementation of the above approach:

## C++

`// C++ program to count the number ` `// of subarrays such that Xor of ` `// all the elements of that subarray ` `// is equal to sum of the elements ` ` ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to count the number ` `// of subarrays such that Xor of ` `// all the elements of that subarray ` `// is equal to sum of the elements ` `ll operation(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Maintain two pointers ` ` ` `// left and right ` ` ` `ll right = 0, ans = 0, ` ` ` `num = 0; ` ` ` ` ` `// Iterating through the array ` ` ` `for` `(ll left = 0; left < N; left++) { ` ` ` ` ` `// Calculate the window ` ` ` `// where the above condition ` ` ` `// is satisfied ` ` ` `while` `(right < N ` ` ` `&& num + arr[right] ` ` ` `== (num ^ arr[right])) { ` ` ` `num += arr[right]; ` ` ` `right++; ` ` ` `} ` ` ` ` ` `// Count will be (right-left) ` ` ` `ans += right - left; ` ` ` `if` `(left == right) ` ` ` `right++; ` ` ` ` ` `// Remove the previous element ` ` ` `// as it is already included ` ` ` `else` ` ` `num -= arr[left]; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4, 5 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << operation(arr, N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the number ` `// of subarrays such that Xor of all ` `// the elements of that subarray is ` `// equal to sum of the elements ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count the number ` `// of subarrays such that Xor of ` `// all the elements of that subarray ` `// is equal to sum of the elements ` `static` `long` `operation(` `int` `arr[], ` `int` `N) ` `{ ` ` ` ` ` `// Maintain two pointers ` ` ` `// left and right ` ` ` `int` `right = ` `0` `; ` ` ` `int` `num = ` `0` `; ` ` ` `long` `ans = ` `0` `; ` ` ` ` ` `// Iterating through the array ` ` ` `for` `(` `int` `left = ` `0` `; left < N; left++) ` ` ` `{ ` ` ` ` ` `// Calculate the window ` ` ` `// where the above condition ` ` ` `// is satisfied ` ` ` `while` `(right < N && num + arr[right] == ` ` ` `(num ^ arr[right])) ` ` ` `{ ` ` ` `num += arr[right]; ` ` ` `right++; ` ` ` `} ` ` ` ` ` `// Count will be (right-left) ` ` ` `ans += right - left; ` ` ` `if` `(left == right) ` ` ` `right++; ` ` ` ` ` `// Remove the previous element ` ` ` `// as it is already included ` ` ` `else` ` ` `num -= arr[left]; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `}; ` ` ` `int` `N = arr.length; ` ` ` ` ` `System.out.println(operation(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the number ` `# of subarrays such that Xor of ` `# all the elements of that subarray ` `# is equal to sum of the elements ` ` ` `# Function to count the number ` `# of subarrays such that Xor of ` `# all the elements of that subarray ` `# is equal to sum of the elements ` `def` `operation(arr, N): ` ` ` ` ` `# Maintain two pointers ` ` ` `# left and right ` ` ` `right ` `=` `0` `; ans ` `=` `0` `; ` ` ` `num ` `=` `0` `; ` ` ` ` ` `# Iterating through the array ` ` ` `for` `left ` `in` `range` `(` `0` `, N): ` ` ` ` ` `# Calculate the window ` ` ` `# where the above condition ` ` ` `# is satisfied ` ` ` `while` `(right < N ` `and` ` ` `num ` `+` `arr[right] ` `=` `=` ` ` `(num ^ arr[right])): ` ` ` `num ` `+` `=` `arr[right]; ` ` ` `right ` `+` `=` `1` `; ` ` ` ` ` `# Count will be (right-left) ` ` ` `ans ` `+` `=` `right ` `-` `left; ` ` ` `if` `(left ` `=` `=` `right): ` ` ` `right ` `+` `=` `1` `; ` ` ` ` ` `# Remove the previous element ` ` ` `# as it is already included ` ` ` `else` `: ` ` ` `num ` `-` `=` `arr[left]; ` ` ` ` ` `return` `ans; ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `]; ` `N ` `=` `len` `(arr) ` `print` `(operation(arr, N)); ` ` ` `# This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the number ` `// of subarrays such that Xor of all ` `// the elements of that subarray is ` `// equal to sum of the elements ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to count the number ` `// of subarrays such that Xor of ` `// all the elements of that subarray ` `// is equal to sum of the elements ` `static` `long` `operation(` `int` `[]arr, ` `int` `N) ` `{ ` ` ` ` ` `// Maintain two pointers ` ` ` `// left and right ` ` ` `int` `right = 0; ` ` ` `int` `num = 0; ` ` ` `long` `ans = 0; ` ` ` ` ` `// Iterating through the array ` ` ` `for` `(` `int` `left = 0; left < N; left++) ` ` ` `{ ` ` ` ` ` `// Calculate the window ` ` ` `// where the above condition ` ` ` `// is satisfied ` ` ` `while` `(right < N && ` ` ` `num + arr[right] == ` ` ` `(num ^ arr[right])) ` ` ` `{ ` ` ` `num += arr[right]; ` ` ` `right++; ` ` ` `} ` ` ` ` ` `// Count will be (right-left) ` ` ` `ans += right - left; ` ` ` `if` `(left == right) ` ` ` `right++; ` ` ` ` ` `// Remove the previous element ` ` ` `// as it is already included ` ` ` `else` ` ` `num -= arr[left]; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 2, 3, 4, 5 }; ` ` ` `int` `N = arr.Length; ` ` ` ` ` `Console.WriteLine(operation(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** *O(N)*, where N is the length of the array.

## Recommended Posts:

- Count subarrays with equal number of 1's and 0's
- Count subarrays with equal number of occurrences of two given elements
- Count of subarrays whose product is equal to difference of two different numbers
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Number of subarrays with GCD equal to 1
- Number of subarrays having sum exactly equal to k
- Number of subarrays such that XOR of one half is equal to the other
- Split array in three equal sum subarrays
- Number of subarrays for which product and sum are equal
- Split an array into two equal Sum subarrays
- Check if it possible to partition in k subarrays with equal sum
- Find if array can be divided into two subarrays of equal sum
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Partitioning into two contiguous element subarrays with equal sums
- Make sum of all subarrays of length K equal by only inserting elements
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Find an element which divides the array in two subarrays with equal product
- Count of subarrays with sum at least K
- Count of all possible bitonic subarrays
- Count the number of subarrays having a given XOR

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.