human
Maximum sum alternating subsequence | Return sum of maximum sum alternating sequence starting with arr [ 0 ] and is first decreasing ..
handling the edge case.
Stores sum of decreasing and increasing sub - sequence.
store sum of increasing and decreasing sun - sequence.
As per question , first element must be part of solution ..
Traverse remaining elements of array.
IF current sub - sequence is decreasing the update dec [ j ] if needed . dec [ i ] by current inc [ j ] + arr [ i ].
Revert the flag , if first decreasing is found.
If next element is greater but flag should be 1 i . e . this element should be counted after the first decreasing element gets counted.
If current sub - sequence is increasing then update inc [ i ].
Find maximum sum in b / w inc [ ] and dec [ ].
Return maximum sum alternate sun - sequence.
Driver program"
How can the above be solved in Python?
 ... Give me a code to maximum sum alternating subsequence and the other steps required?
assistant
#Here is code to maximum sum alternating subsequence and the other steps required.
 def maxAlternateSum ( arr , n ) :
	if ( n == 1 ) :
		return arr [ 0 ]
	min = arr [ 0 ]
	for i in range ( 1 , n ) :
		if ( min > arr [ i ] ) :
			min = arr [ i ]
	if ( arr [ 0 ] == min ) :
		return arr [ 0 ]
	dec = [ 0 for i in range ( n + 1 ) ]
	inc = [ 0 for i in range ( n + 1 ) ]
	dec [ 0 ] = inc [ 0 ] = arr [ 0 ]
	flag = 0
	for i in range ( 1 , n ) :
		for j in range ( i ) :
			if ( arr [ j ] > arr [ i ] ) :
				dec [ i ] = max ( dec [ i ] , inc [ j ] + arr [ i ] )
				flag = 1
			elif ( arr [ j ] < arr [ i ] and flag == 1 ) :
				inc [ i ] = max ( inc [ i ] , dec [ j ] + arr [ i ] )
	result = - 2147483648
	for i in range ( n ) :
		if ( result < inc [ i ] ) :
			result = inc [ i ]
		if ( result < dec [ i ] ) :
			result = dec [ i ]
	return result
arr = [ 8 , 2 , 3 , 5 , 7 , 9 , 10 ]
n = len ( arr )
print ( " Maximum ▁ sum ▁ = ▁ " , maxAlternateSum ( arr , n ) )