human
Give me a code to longest subsequence such that difference between adjacents is one and the other steps required given the following:
Longest subsequence such that difference between adjacents is one | Function to find the length of longest subsequence.
Initialize the dp [ ] array with 1 as a single element will be of 1 length.
Start traversing the given array.
Compare with all the previous elements.
If the element is consecutive then consider this subsequence and update dp [ i ] if required ..
Longest length will be the maximum value of dp array ..
Driver code.
Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }"
How can the above be solved in Python?
assistant
#Here is code to longest subsequence such that difference between adjacents is one and the other steps required.
 def longestSubseqWithDiffOne ( arr , n ) :
	dp = [ 1 for i in range ( n ) ]
	for i in range ( n ) :
		for j in range ( i ) :
			if ( ( arr [ i ] == arr [ j ] + 1 ) or ( arr [ i ] == arr [ j ] - 1 ) ) :
				dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 )
	result = 1
	for i in range ( n ) :
		if ( result < dp [ i ] ) :
			result = dp [ i ]
	return result
arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ]
n = len ( arr )
print longestSubseqWithDiffOne ( arr , n )