Given three arrays **arr1[], arr2[] **and arr3[] of length N1N2 and N3 respectively, the task is to find the maximum sum possible by adding the products of pairs taken from different arrays.

Note: Each array element can be a part of a single pair.

Examples:

Input:_ arr1[] = {3, 5}, arr2[] = {2, 1}, arr3[] = {4, 3, 5}_

_Output : _43

Explanation

After sorting the arrays in descending order, following modifications are obtained: arr1[] = {5, 3}, arr2[] = {2, 1}, arr3[] = {5, 4, 3}.

Therefore, maximized product = (arr1[0] * arr3[0]) + (arr1[1] * arr3[1]) + (arr2[0] * arr3[2]) = (55 + 34 + 2*3) = 43

Input:_ arr1[] = {3, 5, 9, 8, 7}, arr2[] = {6}, arr3[] = {3, 5}_

_Output : _115

Explanation

Sort the arrays in descending order, the following modifications are obtained: arr1[] = {9, 8, 7, 5, 3}, arr2[] = {6}, arr3[] = {5, 3}.

Therefore, maximized product = (arr1[0] * arr2[0]) + (arr1[1] * arr3[0]) + (arr1[2] * arr3[1]) = (96 + 85 + 7*3) = 155

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach: **The given problem can be solved using 3D Memoization table to store the maximum sums for all possible combinations of pairs. Suppose i, j, k are the number of elements taken from the arrays arr1[], arr2[] and arr3[] respectively to form pairs, then the memorization table dp[][][] will store the maximum possible sum of products generated from this combination of elements in dp[i][j][k].

Follow the below steps to solve the problem

  • Sort the given arrays in descending order.
  • Initialize a dp table dp[][][], where dp[i][j][k] store the maximum sum obtained by taking i largest numbers from the first array, j largest numbers from the second array, and k largest numbers from the third array.
  • For every ij, and kth element from the three arrays respectively, check for all possible pairs and calculate the maximum sum possible by considering each pair and memoize the maximum sum obtained for further computation.
  • Finally, print the maximum possible sum returned by the dp[][][] matrix.

Below is the implementation of the above approach:

  • C++

// C++ Program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

#define maxN 201

// Variables which represent

// the size of the array

**int** n1, n2, n3;

// Stores the results

**int** dp[maxN][maxN][maxN];

// Function to return the

// maximum possible sum

**int** getMaxSum(``**int** i, **int** j,

**int** k, **int** arr1[],

**int** arr2[], **int** arr3[])

{

// Stores the count of

// arrays processed

**int** cnt = 0;

**if** (i >= n1)

cnt++;

**if** (j >= n2)

cnt++;

**if** (k >= n3)

cnt++;

// If more than two arrays

// have been processed

**if** (cnt >= 2)

**return** 0;

// If an already computed

// subproblem occurred

**if** (dp[i][j][k] != -1)

**return** dp[i][j][k];

**int** ans = 0;

// Explore all the possible pairs

**if** (i < n1 && j < n2)

// Recursive function call

ans = max(ans,

getMaxSum(i + 1, j + 1, k,

arr1, arr2, arr3)

+ arr1[i] * arr2[j]);

**if** (i < n1 && k < n3)

ans = max(ans,

getMaxSum(i + 1, j, k + 1,

arr1, arr2, arr3)

+ arr1[i] * arr3[k]);

**if** (j < n2 && k < n3)

ans = max(ans,

getMaxSum(i, j + 1, k + 1,

arr1, arr2, arr3)

+ arr2[j] * arr3[k]);

// Memoize the maximum

dp[i][j][k] = ans;

// Returning the value

**return** dp[i][j][k];

}

// Function to return the maximum sum of

// products of pairs possible

**int** maxProductSum(``**int** arr1[], **int** arr2[],

**int** arr3[])

{

// Initialising the dp array to -1

**memset**``(dp, -1, **sizeof**``(dp));

// Sort the arrays in descending order

sort(arr1, arr1 + n1);

reverse(arr1, arr1 + n1);

sort(arr2, arr2 + n2);

reverse(arr2, arr2 + n2);

sort(arr3, arr3 + n3);

reverse(arr3, arr3 + n3);

**return** getMaxSum(0, 0, 0,

arr1, arr2, arr3);

}

// Driver Code

**int** main()

{

n1 = 2;

**int** arr1[] = { 3, 5 };

n2 = 2;

**int** arr2[] = { 2, 1 };

n3 = 3;

**int** arr3[] = { 4, 3, 5 };

cout << maxProductSum(arr1, arr2, arr3);

**return** 0;

}

Output:

43

_Time Complexity: _O((N1 * N2 * N3))

Auxiliary Space:_ O(N1 * N2 * N3)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #dynamic programming #mathematical #matrix #sorting #memoization

Maximize sum of pairwise products generated from the given Arrays
2.45 GEEK