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 ; Dictionary 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 ] ; Retrieve the longest length of subsequence ending at integer [ ] a ^ K ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in Dictionary ; Return the ans if ans >= 2. Otherwise , return 0 ; Input ; Print the length of the longest subsequence
function xorSubsequence ( a , n , k ) { var ans = 0 ; var map = new Map ( ) ; var dp = Array ( n ) . fill ( 0 ) ; map . set ( a [ 0 ] , 1 ) dp [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { var dpj ; if ( map . has ( a [ i ] ^ k ) ) { dpj = map . get ( a [ i ] ^ k ) ; } else { dpj = - 1 ; } if ( dpj != 0 ) dp [ i ] = Math . max ( dp [ i ] , dpj + 1 ) ; ans = Math . max ( ans , dp [ i ] ) ; if ( map . has ( a [ i ] ) ) { map . set ( a [ i ] , Math . max ( map . get ( a [ i ] ) + 1 , dp [ i ] ) ) ; } else { map . set ( a [ i ] , Math . max ( 1 , dp [ i ] ) ) ; } } return ans >= 2 ? ans : 0 ; } var arr = [ 3 , 2 , 4 , 3 , 5 ] ; var N = arr . length ; var K = 1 ; document . write ( xorSubsequence ( arr , N , K ) ) ;