Maximum Sum of Products of Two Arrays in C++

C++Server Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

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
Updated on 09-Sep-2020 13:18:27