human
Maximum subarray sum possible after removing at most one subarray | Function to find maximum subarray sum possible after removing at most one subarray.
Calculate the preSum [ ] array.
Update the value of sum.
Update the value of maxSum.
Update the value of preSum [ i ].
Calculate the postSum [ ] array.
Update the value of sum.
Update the value of maxSum.
Update the value of postSum [ i ].
Stores the resultant maximum sum of subarray.
Update the value of ans.
Print resultant sum.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to maximum subarray sum possible after removing at most one subarray and the other steps required.
 def maximumSum ( arr , n ) :
	preSum = [ 0 ] * n
	sum = 0
	maxSum = 0
	for i in range ( n ) :
		sum = max ( arr [ i ] , sum + arr [ i ] )
		maxSum = max ( maxSum , sum )
		preSum [ i ] = maxSum
	sum = 0
	maxSum = 0
	postSum = [ 0 ] * ( n + 1 )
	for i in range ( n - 1 , - 1 , - 1 ) :
		sum = max ( arr [ i ] , sum + arr [ i ] )
		maxSum = max ( maxSum , sum )
		postSum [ i ] = maxSum
	ans = 0
	for i in range ( n ) :
		ans = max ( ans , preSum [ i ] + postSum [ i + 1 ] )
	print ( ans )
arr = [ 7 , 6 , - 1 , - 4 , - 5 , 7 , 1 ]
N = len ( arr )
maximumSum ( arr , N )