Count subarrays with sum equal to its XOR value | 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
def operation ( arr , N ) : NEW_LINE INDENT right = 0 ; ans = 0 ; NEW_LINE num = 0 ; NEW_LINE for left in range ( 0 , N ) : NEW_LINE INDENT while ( right < N and num + arr [ right ] == ( num ^ arr [ right ] ) ) : NEW_LINE INDENT num += arr [ right ] ; NEW_LINE right += 1 ; NEW_LINE DEDENT ans += right - left ; NEW_LINE if ( left == right ) : NEW_LINE INDENT right += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT num -= arr [ left ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE N = len ( arr ) NEW_LINE print ( operation ( arr , N ) ) ; NEW_LINE