''Driver code
def longestSubsequence ( A , N ) : NEW_LINE INDENT L = [ 1 ] * N NEW_LINE hm = { } NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if abs ( A [ i ] - A [ i - 1 ] ) == 1 : NEW_LINE INDENT L [ i ] = 1 + L [ i - 1 ] NEW_LINE DEDENT elif hm . get ( A [ i ] + 1 , 0 ) or hm . get ( A [ i ] - 1 , 0 ) : NEW_LINE INDENT L [ i ] = 1 + max ( hm . get ( A [ i ] + 1 , 0 ) , hm . get ( A [ i ] - 1 , 0 ) ) NEW_LINE DEDENT hm [ A [ i ] ] = L [ i ] NEW_LINE DEDENT return max ( L ) NEW_LINE DEDENT A = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ] NEW_LINE N = len ( A ) NEW_LINE print ( longestSubsequence ( A , N ) ) NEW_LINE