human
Longest alternating subsequence with maximum sum | Set 2 | Function to check the sign of the element.
Function to calculate and return the maximum sum of longest alternating subsequence.
Iterate through the array.
Stores the first element of a sequence of same sign.
Traverse until an element with opposite sign is encountered.
Update the maximum.
Update the maximum sum.
Update i.
Return the maximum sum.
Driver Code"
Solution in Python:
assistant
#Here is code to longest alternating subsequence with maximum sum and the other steps required.
 def sign ( x ) :
	if ( x > 0 ) :
		return 1
	else :
		return - 1
def findMaxSum ( arr , size ) :
	max_sum = 0
	i = 0
	while i < size :
		pres = arr [ i ]
		j = i
		while ( j < size and ( sign ( arr [ i ] ) == sign ( arr [ j ] ) ) ) :
			pres = max ( pres , arr [ j ] )
			j += 1
		max_sum = max_sum + pres
		i = j - 1
		i += 1
	return max_sum
if __name__ == " _ _ main _ _ " :
	arr = [ - 2 , 8 , 3 , 8 , - 4 , - 15 , 5 , - 2 , - 3 , 1 ]
	size = len ( arr )
	print ( findMaxSum ( arr , size ) )