Related Articles
Maximum Sum of Products of Two Arrays
• Difficulty Level : Easy
• Last Updated : 01 Mar, 2021

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++

 `// CPP program to calculate maximum sum``// of products of two arrays``#include``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);` `        ``// 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<

## Java

 `// 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));``    ``}``}`

## Python 3

 `# 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`

C#

``````
// 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
``````

## PHP

 ``

## Javascript

 ``
Output:
`24`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up