text: Maximize sum of pairwise products generated from the given Arrays | JavaScript Program to implement the above approach ; Variables which represent the size of the array ; Stores the results ; Function to return the maximum possible sum ; Stores the count of arrays processed ; If more than two arrays have been processed ; If an already computed subproblem occurred ; Explore all the possible pairs ; Recursive function call ; Memoize the maximum ; Returning the value ; Function to return the maximum sum of products of pairs possible ; Sort the arrays in descending order ; Driver Code
code: var maxN = 201 ; var n1 , n2 , n3 ; var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxN ) ) ; for ( var i = 0 ; i < maxN ; i ++ ) for ( var j = 0 ; j < maxN ; j ++ ) dp [ i ] [ j ] = new Array ( maxN ) . fill ( - 1 ) ; function getMaxSum ( i , j , k , arr1 , arr2 , arr3 ) { var cnt = 0 ; if ( i >= n1 ) cnt ++ ; if ( j >= n2 ) cnt ++ ; if ( k >= n3 ) cnt ++ ; if ( cnt >= 2 ) return 0 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; var ans = 0 ; if ( i < n1 && j < n2 ) ans = Math . max ( ans , getMaxSum ( i + 1 , j + 1 , k , arr1 , arr2 , arr3 ) + arr1 [ i ] * arr2 [ j ] ) ; if ( i < n1 && k < n3 ) ans = Math . max ( ans , getMaxSum ( i + 1 , j , k + 1 , arr1 , arr2 , arr3 ) + arr1 [ i ] * arr3 [ k ] ) ; if ( j < n2 && k < n3 ) ans = Math . max ( ans , getMaxSum ( i , j + 1 , k + 1 , arr1 , arr2 , arr3 ) + arr2 [ j ] * arr3 [ k ] ) ; dp [ i ] [ j ] [ k ] = ans ; return dp [ i ] [ j ] [ k ] ; } function maxProductSum ( arr1 , arr2 , arr3 ) { arr1 . sort ( ) ; arr1 . reverse ( ) ; arr2 . sort ( ) ; arr2 . reverse ( ) ; arr3 . sort ( ) ; arr3 . reverse ( ) ; return getMaxSum ( 0 , 0 , 0 , arr1 , arr2 , arr3 ) ; } n1 = 2 ; var arr1 = [ 3 , 5 ] ; n2 = 2 ; var arr2 = [ 2 , 1 ] ; n3 = 3 ; var arr3 = [ 4 , 3 , 5 ] ; document . write ( maxProductSum ( arr1 , arr2 , arr3 ) ) ;