Minimize the sum of product

You are given two arrays, A and B, of equal size N. The task is to find the minimum value of A[0] * B[0] + A[1] * B[1] +…+ A[N-1] * B[N-1], where shuffling of elements of arrays A and B is allowed.








STUDYEVERTHING







#include <iostream>
#include <bits/stdc++.h>

using namespace std;
int main()
{
    int n;
    cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }

    sort(a, a + n);
    sort(b, b + n);

    int ans = 0;
    int j = n - 1;
    for (int i = 0; i < n; i++)
    {
        ans = ans + (a[i] * b[j]);
        j--;
    }
    cout << ans;
}











Happy Coding 😊.





Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post