Open In App

Maximum Sum of Products of Two Arrays

Last Updated : 19 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Implementation:

C++
// 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
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#
using System;

class Program
{
  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;
  }

  static void Main(string[] args)
  {
    int[] A = { 1, 2, 3 };
    int[] B = { 4, 5, 1 };

    Console.WriteLine(MaximumSOP(A, B));
  }
}

// This code is contributed by shivhack999
PHP
<?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 
?>
JavaScript
<script>

// Javascript program to calculate maximum sum 
// of products of two arrays  

    // Function that calculates maximum sum 
    // of products of two arrays 
    function maximumSOP(a, b) 
    { 
        // Variable to store the sum of 
        // products of array elements 
        let sop = 0; 

        // length of the arrays 
        let n = a.length; 

        // Sorting both the arrays 
        a.sort(); 
        b.sort(); 

        // Traversing both the arrays 
        // and calculating sum of product 
        for (let i = 0; i <n; i++) { 
            sop += (a[i] * b[i]); 
        } 

        return sop; 
    } 

    // Driver code 
        let A = [ 1, 2, 3 ]; 
        let B = [ 4, 5, 1 ]; 

        document.write(maximumSOP(A, B)); 

// This code is contributed by Mayank Tyagi

</script>

Output
24

Complexity Analysis:

  • Time Complexity: O(nlog(n))
  • Auxiliary Space: O(1)

Next Article
Article Tags :
Practice Tags :

Similar Reads