Maximum Sum of Products of Two Arrays in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find maximum Sum of Products of Two Arrays.

For this we will be provided with two arrays of same size. Our task is to find the maximum sum by multiplying exactly one element from first element with one element from the second array.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//calculating maximum sum by
//multiplying elements
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;
}

Output

24
raja
Published on 09-Sep-2020 13:18:27
Advertisements