Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum | Python program for the above approach ; Function to find the maximum product possible after repeatedly replacing pairs of adjacent array elements with their sum ; Store the maximum product ; Store the prefix sum ; Store the total sum of array ; Traverse the array to find the total sum ; Iterate in the range [ 0 , N - 2 ] ; Add arr [ i ] to prefix_sum ; Store the value of prefix_sum ; Store the value of ( total sum - prefix sum ) ; Update the maximum product ; Print the answer ; Driver Code
import sys NEW_LINE def maxProduct ( arr , N ) : NEW_LINE INDENT max_product = - sys . maxsize ; NEW_LINE prefix_sum = 0 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT for i in range ( N - 1 ) : NEW_LINE INDENT prefix_sum += arr [ i ] ; NEW_LINE X = prefix_sum ; NEW_LINE Y = sum - prefix_sum ; NEW_LINE max_product = max ( max_product , X * Y ) ; NEW_LINE DEDENT print ( max_product ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 6 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maxProduct ( arr , N ) ; NEW_LINE DEDENT