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
def longestAlternativeSequence ( a , n ) : NEW_LINE INDENT maxi1 = 0 NEW_LINE f1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( f1 == 0 ) : NEW_LINE INDENT if ( a [ i ] % 2 ) : NEW_LINE INDENT f1 = 1 NEW_LINE maxi1 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT maxi1 += 1 NEW_LINE f1 = 0 NEW_LINE DEDENT DEDENT DEDENT maxi2 = 0 NEW_LINE f2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( f2 ) : NEW_LINE INDENT if ( a [ i ] % 2 ) : NEW_LINE INDENT f2 = 1 NEW_LINE maxi2 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT maxi2 += 1 NEW_LINE f2 = 0 NEW_LINE DEDENT DEDENT DEDENT return max ( maxi1 , maxi2 ) NEW_LINE DEDENT a = [ 13 , 16 , 8 , 9 , 32 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( longestAlternativeSequence ( a , n ) ) NEW_LINE