Length of the longest subsequence such that XOR of adjacent elements is equal to K | Function to find maximum length of subsequence ; Stores maximum length of subsequence ; HashMap to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in HashMap ; Return the ans if ans >= 2. Otherwise , return 0 ; Driver Code ; Input ; Print the length of the longest subsequence
def xorSubsequence ( a , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE map = { } NEW_LINE dp = [ 0 ] * n NEW_LINE map [ a [ 0 ] ] = 1 NEW_LINE dp [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] ^ k in map ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , map [ a [ i ] ^ k ] + 1 ) NEW_LINE DEDENT ans = max ( ans , dp [ i ] ) NEW_LINE if a [ i ] in map : NEW_LINE INDENT map [ a [ i ] ] = max ( map [ a [ i ] ] , dp [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT map [ a [ i ] ] = max ( 1 , dp [ i ] ) NEW_LINE DEDENT DEDENT if ans >= 2 : NEW_LINE return ans NEW_LINE return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE print ( xorSubsequence ( arr , N , K ) ) NEW_LINE DEDENT