human
Longest Consecuetive Subsequence when only one insert operation is allowed | Function to return the length of longest consecuetive subsequence after inserting an element.
Variable to find maximum value of the array.
Calculating maximum value of the array.
Declaring the DP table.
Variable to store the maximum length.
Iterating for every value present in the array.
Recurrence for dp [ val ] [ 0 ].
No value can be inserted before 1 , hence the element value should be greater than 1 for this recurrance relation.
Recurrence for dp [ val ] [ 1 ].
Maximum length of consecutive sequence ending at 1 is equal to 1.
Update the ans variable with the new maximum length possible.
Return the ans.
Input array"
How can the above be solved in Python?
 ... What is a code to longest Consecuetive Subsequence when only one insert operation is allowed and the other steps required?
assistant
#Here is code to longest Consecuetive Subsequence when only one insert operation is allowed and the other steps required.
 def LongestConsSeq ( arr , N ) :
	maxval = 1
	for i in range ( N ) :
		maxval = max ( maxval , arr [ i ] )
	dp = [ [ 0 for i in range ( 2 ) ] for i in range ( maxval + 1 ) ]
	ans = 1
	for i in range ( N ) :
		dp [ arr [ i ] ] [ 0 ] = 1 + dp [ arr [ i ] - 1 ] [ 0 ]
		if ( arr [ i ] >= 2 ) :
			dp [ arr [ i ] ] [ 1 ] = max ( 1 + dp [ arr [ i ] - 1 ] [ 1 ] , 2 + dp [ arr [ i ] - 2 ] [ 0 ] )
		else :
			dp [ arr [ i ] ] [ 1 ] = 1
		ans = max ( ans , dp [ arr [ i ] ] [ 1 ] )
	return ans
arr = [ 2 , 1 , 4 , 5 ]
N = len ( arr )
print ( LongestConsSeq ( arr , N ) )