human
Queries to find longest subsequence having no similar adjacent elements with updates | Python3 program for the above approach.
Traverse the array arr [ ].
If previous element is not same as current element.
Traverse the queries.
Replace element at index x with y.
Recalculate for index x.
Subtract contribution of element at index x.
Add contribution of y.
Recalculate for index x + 1.
Subtract contribution of element at index x + 1.
Adds contribution of y.
Replace the element.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to queries to find longest subsequence having no similar adjacent elements with updates and the other steps required.
 def longestSubsequence ( N , Q , arr , Queries ) :
	count = 1
	for i in range ( 1 , N ) :
		if ( arr [ i ] != arr [ i - 1 ] ) :
			count += 1
	for i in range ( Q ) :
		x = Queries [ i ] [ 0 ]
		y = Queries [ i ] [ 1 ]
		if ( x > 1 ) :
			if ( arr [ x - 1 ] != arr [ x - 2 ] ) :
				count -= 1
			if ( arr [ x - 2 ] != y ) :
				count += 1
		if ( x < N ) :
			if ( arr [ x ] != arr [ x - 1 ] ) :
				count -= 1
			if ( y != arr [ x ] ) :
				count += 1
		print ( count , end = ' ▁ ' )
		arr [ x - 1 ] = y
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 1 , 2 , 5 , 2 ]
	N = len ( arr )
	Q = 2
	Queries = [ [ 1 , 3 ] , [ 4 , 2 ] ]
	longestSubsequence ( N , Q , arr , Queries )