Maximum Sum of Products of Two Arrays

Given two arrays A and B of positive integers of the same size N. The task is to find the maximum sum of products of their elements. Each element in A has to be multiplied with exactly one element in B or vice versa such that each element of both the arrays appears exactly once and the sum of the product obtained is maximum.

Examples:

Input : A[] = {1, 2, 3}
B[] = {4, 5, 1}
Output : 24
Explanation : Maximum sum of product is obtained by 5*3+4*2+1*1 = 24.



Input : A[] = {5, 1, 3, 4, 2}
B[] = {8, 10, 9, 7, 6}
Output : 130
Explanation : Maximum sum of product is obtained by 10*5+9*4+8*3+7*2+6*1 = 130.

The idea is to observe that product of two maximum number will contribute toward the maximum sum of the product. So the idea is to:

  1. Sort both the arrays.
  2. Traverse the arrays, and calculate the sum of products of array elements that are at the same index.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to calculate maximum sum
// of products of two arrays
#include<bits/stdc++.h>
using namespace std;
  
    // Function that calculates maximum sum
    // of products of two arrays
    int maximumSOP(int *a, int *b)
    {
        // Variable to store the sum of
        // products of array elements
        int sop = 0;
  
        // length of the arrays
        int n = sizeof(a)/sizeof(a[0]); 
  
        // Sorting both the arrays
        sort(a,a+n+1);
        sort(b,b+n+1);
  
        // Traversing both the arrays
        // and calculating sum of product
        for (int i = 0; i <=n; i++) {
            sop += a[i] * b[i];
        }
  
        return sop;
    }
  
    // Driver code
    int main()
    {
        int A[] = { 1, 2, 3 };
        int B[] = { 4, 5, 1 };
  
        cout<<maximumSOP(A, B);
        return 0;
    }
  
// This code is contributed by mits

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to calculate maximum sum
// of products of two arrays
  
import java.io.*;
import java.util.*;
public class GFG {
  
    // Function that calculates maximum sum
    // of products of two arrays
    static int maximumSOP(int[] a, int[] b)
    {
        // Variable to store the sum of
        // products of array elements
        int sop = 0;
  
        // length of the arrays
        int n = a.length;
  
        // Sorting both the arrays
        Arrays.sort(a);
        Arrays.sort(b);
  
        // Traversing both the arrays
        // and calculating sum of product
        for (int i = 0; i < n; i++) {
            sop += a[i] * b[i];
        }
  
        return sop;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int[] A = { 1, 2, 3 };
        int[] B = { 4, 5, 1 };
  
        System.out.println(maximumSOP(A, B));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to calculate 
# maximum sum of products of 
# two arrays 
  
# Function that calculates 
# maximum sum of products 
# of two arrays 
def maximumSOP(a, b) :
  
    # Variable to store the sum of 
    # products of array elements 
    sop = 0
  
    # length of the arrays 
    n = len(a)
  
    # Sorting both the arrays 
    a.sort()
    b.sort()
  
    # Traversing both the arrays 
    # and calculating sum of product
    for i in range(n) :
        sop += a[i] * b[i]
  
    return sop
  
# Driver code     
if __name__ == "__main__" :
  
    A = [1, 2, 3]
    B = [4, 5, 1]
  
    print(maximumSOP(A, B))
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate maximum sum
// of products of two arrays
using System;
  
class GFG 
{
  
// Function that calculates maximum 
// sum of products of two arrays
static int maximumSOP(int[] a, int[] b)
{
    // Variable to store the sum of
    // products of array elements
    int sop = 0;
  
    // length of the arrays
    int n = a.Length;
  
    // Sorting both the arrays
    Array.Sort(a);
    Array.Sort(b);
  
    // Traversing both the arrays
    // and calculating sum of product
    for (int i = 0; i < n; i++) 
    {
        sop += a[i] * b[i];
    }
  
    return sop;
}
  
// Driver code
public static void Main()
{
    int[] A = { 1, 2, 3 };
    int[] B = { 4, 5, 1 };
  
    Console.Write(maximumSOP(A, B));
}
}
  
// This code is contributed 
// by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate maximum  
// sum of products of two arrays 
  
// Function that calculates maximum 
// sum of products of two arrays 
function maximumSOP(&$a, &$b
    $sop = 0;
      
    // Sorting both the arrays 
    sort($a); 
    sort($b); 
  
    // length of the arrays 
    $n = sizeof($a); 
      
    // Traversing both the arrays 
    // and calculating sum of product 
    for ($i = 0; $i < $n; $i++) 
    
        $sop = $sop + ($a[$i] * $b[$i]); 
    
  
    return $sop
  
// Driver code 
$A = array(1, 2, 3 ); 
$B = array(4, 5, 1 ); 
echo maximumSOP($A, $B); 
  
// This code is contributed
// by Shivi_Aggarwal 
?>

chevron_right


Output:

24


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.