Open In App

JavaScript Program for Maximum Possible Sum of Products

In this article, we will find and print the maximum possible sum of products in the JavaScript language. We have two arrays, inputArr1, and inputArr2. Both these arrays contain positive numbers and the length of both arrays is similar. We have to write the function where every element in inputArr1 has to be multiplied with exactly one element in the inputArr2 array or vice versa, such that each element from both the arrays should appear exactly once and the sum of their multiplication should be maximum.

Examples:



Input: inputArr1[] = [1, 2, 3], inputArr2[] = [5, 4, 6]
Output: 32
Explanation: The maximum sum of product is obtained by 3*6+2*5+1*4 = 32

Let’s go through all three approaches with the implementation.

Approach 1: Using Greedy Algorithm in JavaScript

Here, we are going to use the Greedy Algorithm to find the maximum possible sum of products. We are actually finding the maximum elements from the arrays and then we are multiplying these maximum elements with each other. Using the Infinity we are making sure that each value is used only once.



Example: This example shows the use of the above-explained approach.




function maxSumProdUsingGreedy(inputArr1, inputArr2) {
    if (inputArr1.length !== inputArr2.length) {
        return -1;
    }
    let arr1Len = inputArr1.length;
    let outputSum = 0;
  
    for (let i = 0; i < arr1Len; i++) {
        let max1Value = -Infinity;
        let max2Value = -Infinity;
        let index1 = -1;
        let index2 = -1;
        for (let j = 0; j < arr1Len; j++) {
            if (inputArr1[j] > max1Value) {
                max1Value = inputArr1[j];
                index1 = j;
            }
            if (inputArr2[j] > max2Value) {
                max2Value = inputArr2[j];
                index2 = j;
            }
        }
        outputSum += max1Value * max2Value;
        inputArr1[index1] = -Infinity;
        inputArr2[index2] = -Infinity;
    }
  
    return outputSum;
}
  
let array1 = [1, 2, 3];
let array2 = [5, 4, 6];
let result1 = maxSumProdUsingGreedy(array1, array2);
console.log(result1);
  
let array3 = [4, 7, 5, 2];
let array4 = [2, 3, 2, 1];
let result2 = maxSumProdUsingGreedy(array3, array4);
console.log(result2);

Output
32
41

Time Complexity: O(n^2)

Space Complexity: O(1)

Approach 2: Using Sorting and Multiplying

In this approach, we are using the sorting approach. As we need to multiply the maximum element from both the arrays, initially, we sort the arrays in descending order, and then we multiply them with each other by taking the index of elements as the reference.

Example: This example shows the use of the above-explained approach.




function maxSumProdUsingSortMult(inputArr1, inputArr2) {
    if (inputArr1.length !== inputArr2.length) {
        return -1;
    }
    inputArr1.sort((a, b) => b - a);
    inputArr2.sort((a, b) => b - a);
  
    let outputSum = 0;
    for (let i = 0; i < inputArr1.length; i++) {
        outputSum += inputArr1[i] * inputArr2[i];
    }
  
    return outputSum;
}
let array1 = [1, 2, 3];
let array2 = [5, 4, 6];
let result1 = 
    maxSumProdUsingSortMult(array1, array2);
console.log(result1);
  
let array3 = [4, 7, 5, 2];
let array4 = [2, 3, 2, 1];
let result2 = 
    maxSumProdUsingSortMult(array3, array4);
console.log(result2);

Output
32
41

Time Complexity: O(n log n)

Space Complexity: O(1)

Approach 3: Using the JavaScript reduce method

In this approach, we are going to use the inbuilt method of JavaScript that is reduce method which will help to go through one of the input arrays (inputArr1) by calculating the sum of the products by having the multiplication with the elements from both the input arrays at the same index. This will give us the maximum sum of products.

Example: This example shows the use of the above-explained approach.




function maxSumProdUsingReduce(inputArr1, inputArr2) {
    if (inputArr1.length !== inputArr2.length) {
        return -1;
    }
  
    inputArr1.sort((a, b) => b - a);
    inputArr2.sort((a, b) => b - a);
  
    let outputSum = inputArr1.reduce(
        (sum, value, index) => {
            return sum + value * inputArr2[index];
        },
        0
    );
  
    return outputSum;
}
  
let array1 = [1, 2, 3];
let array2 = [5, 4, 6];
let result1 = 
    maxSumProdUsingReduce(array1, array2);
console.log(result1);
  
let array3 = [4, 7, 5, 2];
let array4 = [2, 3, 2, 1];
let result2 = 
    maxSumProdUsingReduce(array3, array4);
console.log(result2);

Output
32
41

Time Complexity: O(n log n)

Space Complexity: O(1)


Article Tags :