Maximum Sum of Products of Two Arrays in C++
https://www.tutorialspoint.com/maximum-sum-of-products-of-two-arrays-in-cplusplus
-
10-09-2020 - |
質問
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
#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
所属していません Tutorialspoint