Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum | 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
function maxProduct ( arr , N ) { var max_product = Number . MIN_VALUE ; var prefix_sum = 0 ; var sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( i = 0 ; i < N - 1 ; i ++ ) { prefix_sum += arr [ i ] ; var X = prefix_sum ; var Y = sum - prefix_sum ; max_product = Math . max ( max_product , X * Y ) ; } document . write ( max_product ) ; } var arr = [ 2 , 3 , 5 , 6 , 7 ] ; var N = arr . length ; maxProduct ( arr , N ) ;