Remove array end element to maximize the sum of product | Javascript program to find maximum score we can get by removing elements from either end . ; If only one element left . ; If already calculated , return the value . ; Computing Maximum value when element at index i and index j is to be choosed . ; driver function
let MAX = 50 ; function solve ( dp , a , low , high , turn ) { if ( low == high ) { return Math . floor ( a [ low ] * turn ) ; } if ( dp [ low ] [ high ] != 0 ) { return dp [ low ] [ high ] ; } dp [ low ] [ high ] = Math . max ( Math . floor ( a [ low ] * turn ) + solve ( dp , a , low + 1 , high , turn + 1 ) , Math . floor ( a [ high ] * turn ) + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low ] [ high ] ; } let arr = [ 1 , 3 , 1 , 5 , 2 ] ; let n = arr . length ; let dp = new Array ( MAX ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = 0 ; } } document . write ( solve ( dp , arr , 0 , n - 1 , 1 ) ) ;