human
Maximum length Subsequence with alternating sign and maximum Sum | Function to find the subsequence with alternating sign having maximum size and maximum sum ..
Find whether each element is positive or negative.
Find the required subsequence.
Find the maximum element in the specified range.
print the result.
Driver code.
array declaration.
size of array"
Solution in Python:
 ... Give me a code to maximum length Subsequence with alternating sign and maximum Sum and the other steps required?
assistant
#Here is code to maximum length Subsequence with alternating sign and maximum Sum and the other steps required.
 def findSubsequence ( arr , n ) :
	sign = [ 0 ] * n
	for i in range ( n ) :
		if ( arr [ i ] > 0 ) :
			sign [ i ] = 1
		else :
			sign [ i ] = - 1
	k = 0
	result = [ 0 ] * n
	i = 0
	while i < n :
		cur = arr [ i ]
		j = i
		while ( j < n and sign [ i ] == sign [ j ] ) :
			cur = max ( cur , arr [ j ] )
			j += 1
		result [ k ] = cur
		k += 1
		i = j - 1
		i += 1
	for i in range ( k ) :
		print ( result [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 ]
	n = len ( arr )
	findSubsequence ( arr , n )