human
Maximum sum in circular array such that no two elements are adjacent | Function to calculate the sum from 0 th position to ( n - 2 ) th position.
copy the element of original array to dp [ ].
find the maximum element in the array.
start from 2 nd to n - 1 th pos.
traverse for all pairs bottom - up approach.
dp - condition.
find maximum sum.
return the maximum.
Function to find the maximum sum from 1 st position to n - 1 - th position.
Traverse from third to n - th pos.
bootom - up approach.
dp condition.
find max sum.
return max.
Driver Code"
Solution in Python:
 Write me a code to maximum sum in circular array such that no two elements are adjacent and the other steps required?
assistant
#Here is code to maximum sum in circular array such that no two elements are adjacent and the other steps required.
 def maxSum1 ( arr , n ) :
	dp = [ 0 ] * n
	maxi = 0
	for i in range ( n - 1 ) :
		dp [ i ] = arr [ i ]
		if ( maxi < arr [ i ] ) :
			maxi = arr [ i ]
	for i in range ( 2 , n - 1 ) :
		for j in range ( i - 1 ) :
			if ( dp [ i ] < dp [ j ] + arr [ i ] ) :
				dp [ i ] = dp [ j ] + arr [ i ]
				if ( maxi < dp [ i ] ) :
					maxi = dp [ i ]
	return maxi
def maxSum2 ( arr , n ) :
	dp = [ 0 ] * n
	maxi = 0
	for i in range ( 1 , n ) :
		dp [ i ] = arr [ i ]
		if ( maxi < arr [ i ] ) :
			maxi = arr [ i ]
	for i in range ( 3 , n ) :
		for j in range ( 1 , i - 1 ) :
			if ( dp [ i ] < arr [ i ] + dp [ j ] ) :
				dp [ i ] = arr [ i ] + dp [ j ]
				if ( maxi < dp [ i ] ) :
					maxi = dp [ i ]
	return maxi
def findMaxSum ( arr , n ) :
	return max ( maxSum1 ( arr , n ) , maxSum2 ( arr , n ) )
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 3 , 1 ]
	n = len ( arr )
	print ( findMaxSum ( arr , n ) )