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