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.
- Generate all the subarray
- Calculate the subarray sum and xor to all its elements
- Check if the subarray sum is equal to xor of all its elements then increment the count
- Finally, return count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int operation( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n ; i++){
int sum = 0;
int xorr = 0;
for ( int j = i; j < n; j++){
sum += arr[j];
xorr ^= arr[j];
if (sum == xorr) count++;
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << operation(arr, N);
}
|
Java
import java.util.*;
class Gfg {
public static int operation( int [] arr, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int sum = 0 ;
int xorr = 0 ;
for ( int j = i; j < n; j++) {
sum += arr[j];
xorr ^= arr[j];
if (sum == xorr)
count++;
}
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.println(operation(arr, N));
}
}
|
Python3
def operation(arr, n):
count = 0 ;
for i in range ( 0 ,n):
sum = 0 ;
xorr = 0 ;
for j in range (i,n):
sum + = arr[j];
xorr ^ = arr[j];
if ( sum = = xorr):
count + = 1 ;
return count;
arr = [ 1 , 2 , 3 , 4 , 5 ];
N = len (arr);
print (operation(arr, N));
|
C#
using System;
using System.Linq;
class Gfg {
public static int operation( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int sum = 0;
int xorr = 0;
for ( int j = i; j < n; j++) {
sum += arr[j];
xorr ^= arr[j];
if (sum == xorr)
count++;
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.WriteLine(operation(arr, N));
}
}
|
Javascript
function operation( arr, n)
{
let count = 0;
for (let i = 0; i < n ; i++){
let sum = 0;
let xorr = 0;
for (let j = i; j < n; j++){
sum += arr[j];
xorr ^= arr[j];
if (sum == xorr)
count++;
}
}
return count;
}
let arr = [1, 2, 3, 4, 5 ];
let N = arr.length;
document.write(operation(arr, N));
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the concept of sliding window.
Below implementation uses two pointers, left and right, to define the current subarray. The num variable stores the XOR of all elements in the current subarray, and it is also used to keep track of the sum of the elements in the subarray.
The outer loop iterates left from 0 to N-1, while the inner loop moves right to the right until the condition num + arr[right] == (num ^ arr[right]) is no longer satisfied. The right – left is then added to ans to count the number of subarrays satisfying the condition.
If left == right, right is incremented. Otherwise, the element at index left is removed from num. The process is repeated for the next value of left.
Finally, the function returns ans, which is the number of subarrays satisfying the condition.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll operation( int arr[], int N)
{
ll right = 0, ans = 0,
num = 0;
for (ll left = 0; left < N; left++) {
while (right < N
&& num + arr[right]
== (num ^ arr[right])) {
num += arr[right];
right++;
}
ans += right - left;
if (left == right)
right++;
else
num -= arr[left];
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << operation(arr, N);
}
|
Java
import java.io.*;
class GFG{
static long operation( int arr[], int N)
{
int right = 0 ;
int num = 0 ;
long ans = 0 ;
for ( int left = 0 ; left < N; left++)
{
while (right < N && num + arr[right] ==
(num ^ arr[right]))
{
num += arr[right];
right++;
}
ans += right - left;
if (left == right)
right++;
else
num -= arr[left];
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.println(operation(arr, N));
}
}
|
Python3
def operation(arr, N):
right = 0 ; ans = 0 ;
num = 0 ;
for left in range ( 0 , N):
while (right < N and
num + arr[right] = =
(num ^ arr[right])):
num + = arr[right];
right + = 1 ;
ans + = right - left;
if (left = = right):
right + = 1 ;
else :
num - = arr[left];
return ans;
arr = [ 1 , 2 , 3 , 4 , 5 ];
N = len (arr)
print (operation(arr, N));
|
C#
using System;
class GFG{
static long operation( int []arr, int N)
{
int right = 0;
int num = 0;
long ans = 0;
for ( int left = 0; left < N; left++)
{
while (right < N &&
num + arr[right] ==
(num ^ arr[right]))
{
num += arr[right];
right++;
}
ans += right - left;
if (left == right)
right++;
else
num -= arr[left];
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.WriteLine(operation(arr, N));
}
}
|
Javascript
<script>
function operation(arr, N)
{
let right = 0, ans = 0,
num = 0;
for (let left = 0; left < N; left++) {
while (right < N
&& num + arr[right]
== (num ^ arr[right])) {
num += arr[right];
right++;
}
ans += right - left;
if (left == right)
right++;
else
num -= arr[left];
}
return ans;
}
let arr = [ 1, 2, 3, 4, 5 ];
let N = arr.length;
document.write(operation(arr, N));
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1) because constant space is being used for variables
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...