human
Longest alternative parity subsequence | Function to find the longest.
Marks the starting of odd number as sequence and alternatively changes.
Finding the longest odd / even sequence.
Find odd number.
Find even number.
Length of the longest even / odd sequence.
Find odd number.
Find even number.
Answer is maximum of both odd / even or even / odd subsequence.
Driver Code"
Solution in Python:
assistant
#Here is code to longest alternative parity subsequence and the other steps required.
 def longestAlternativeSequence ( a , n ) :
	maxi1 = 0
	f1 = 0
	for i in range ( n ) :
		if ( f1 == 0 ) :
			if ( a [ i ] % 2 ) :
				f1 = 1
				maxi1 += 1
		else :
			if ( a [ i ] % 2 == 0 ) :
				maxi1 += 1
				f1 = 0
	maxi2 = 0
	f2 = 0
	for i in range ( n ) :
		if ( f2 ) :
			if ( a [ i ] % 2 ) :
				f2 = 1
				maxi2 += 1
		else :
			if ( a [ i ] % 2 == 0 ) :
				maxi2 += 1
				f2 = 0
	return max ( maxi1 , maxi2 )
a = [ 13 , 16 , 8 , 9 , 32 , 10 ]
n = len ( a )
print ( longestAlternativeSequence ( a , n ) )