human
Find the Maximum Alternate Subsequence Sum from a given array | Function to find maximum alternating subsequence sum.
initialize sum to 0.
calculate the sum.
return the final result.
array initiaisation.
length of array"
How can the above be solved in Python?
assistant
#Here is code to find the Maximum Alternate Subsequence Sum from a given array and the other steps required.
 def maxAlternatingSum ( arr , n ) :
	max_sum = 0
	i = 0
	while i < n :
		current_max = arr [ i ]
		k = i
		while k < n and ( ( arr [ i ] > 0 and arr [ k ] > 0 ) or ( arr [ i ] < 0 and arr [ k ] < 0 ) ) :
			current_max = max ( current_max , arr [ k ] )
			k += 1
		max_sum += current_max
		i = k
	return max_sum
arr = [ 1 , 2 , 3 , 4 , - 1 , - 2 ]
n = len ( arr )
print ( maxAlternatingSum ( arr , n ) )