Count subarrays with sum equal to its XOR value | 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 ; 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
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 ) ) ; } }