Find a K | Utility function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Find XOR of whole array ; Find XOR of first K elements ; Adding XOR of next element ; Removing XOR of previous element ; Check if XOR of current subarray matches with the XOR of remaining elements or not ; Function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Driver Code ; Given array ; Size of the array ; Given K ; Function Call
def isSubarrayExistUtil ( arr , K , N ) : NEW_LINE INDENT totalXOR = 0 NEW_LINE SubarrayXOR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalXOR ^= arr [ i ] NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT SubarrayXOR ^= arr [ i ] NEW_LINE DEDENT if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT SubarrayXOR ^= arr [ i ] NEW_LINE SubarrayXOR ^= arr [ i - 1 ] NEW_LINE if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isSubarrayExist ( arr , K , N ) : NEW_LINE INDENT if ( isSubarrayExistUtil ( arr , K , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 5 NEW_LINE isSubarrayExist ( arr , K , N ) NEW_LINE DEDENT