Sum of XOR of all sub | Sum of XOR of all K length sub - array of an array ; If the length of the array is less than k ; Array that will store xor values of subarray from 1 to i ; Traverse through the array ; If i is greater than zero , store xor of all the elements from 0 to i ; If it is the first element ; If i is greater than k ; Xor of values from 0 to i ; Now to find subarray of length k that ends at i , xor sum with x [ i - k ] ; Add the xor of elements from i - k + 1 to i ; Return the resultant sum ; ; Driver code
def FindXorSum ( arr , k , n ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT x = [ 0 ] * n ; NEW_LINE result = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i > 0 ) : NEW_LINE INDENT x [ i ] = x [ i - 1 ] ^ arr [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT x [ i ] = arr [ i ] ; NEW_LINE DEDENT if ( i >= k - 1 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE sum = x [ i ] ; NEW_LINE if ( i - k > - 1 ) : NEW_LINE INDENT sum ^= x [ i - k ] ; NEW_LINE DEDENT result += sum ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = 4 ; k = 2 ; NEW_LINE print ( FindXorSum ( arr , k , n ) ) ; NEW_LINE