Count subarrays with sum equal to its XOR value | 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 ; Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE 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 ) ; }