# Minimum sum of product of two arrays

Find the minimum sum of Products of two arrays of the same size, given that k modifications are allowed on the first array. In each modification, one array element of the first array can either be increased or decreased by 2.

Examples:

```Input : ar[] = {1, 2, -3}
b[]  = {-2, 3, -5}
k = 5
Output : -31
Explanation:
Here n = 3 and k = 5.
So, we modified a[2], which is -3 and
increased it by 10 (as 5 modifications
are allowed).
Final sum will be :
(1 * -2) + (2 * 3) + (7 * -5)
-2    +    6    -    35
-31
(which is the minimum sum of the array
with given conditions)

Input : a[] = {2, 3, 4, 5, 4}
b[] = {3, 4, 2, 3, 2}
Output : 25
Explanation:
Here, total numbers are 5 and total
modifications allowed are 3. So, modify
a[1], which is 3 and decreased it by 6
(as 3 modifications are allowed).
Final sum will be :
(2 * 3) + (-3 * 4) + (4 * 2) + (5 * 3) + (4 * 2)
6    –    12    +    8    +    15   +    8
25
(which is the minimum sum of the array with
given conditions)
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Since we need to minimize the product sum, we find the maximum product and reduce it. By taking some examples, we observe that making 2*k changes to only one element is enough to get the minimum sum. Based on this observation, we consider every element as the element on which we apply all k operations and keep track of the element that reduces result to minimum.

```// CPP program to find minimum sum of product
// of two arrays with k operations allowed on
// first array.
#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum product
int minproduct(int a[], int b[], int n, int k)
{
int diff = 0, res = 0;
int temp;
for (int i = 0; i < n; i++) {

// Find product of current elements and update
// result.
int pro = a[i] * b[i];
res = res + pro;

// If both product and b[i] are negative,
// we must increase value of a[i] to minimize
// result.
if (pro < 0 && b[i] < 0)
temp = (a[i] + 2 * k) * b[i];

// If both product and a[i] are negative,
// we must decrease value of a[i] to minimize
// result.
else if (pro < 0 && a[i] < 0)
temp = (a[i] - 2 * k) * b[i];

// Similar to above two cases for positive
// product.
else if (pro > 0 && a[i] < 0)
temp = (a[i] + 2 * k) * b[i];
else if (pro > 0 && a[i] > 0)
temp = (a[i] - 2 * k) * b[i];

// Check if current difference becomes higher
// than the maximum difference so far.
int d = abs(pro - temp);
if (d > diff)
diff = d;
}

return res - diff;
}

// Driver function
int main()
{
int a[] = { 2, 3, 4, 5, 4 };
int b[] = { 3, 4, 2, 3, 2 };
int n = 5, k = 3;
cout << minproduct(a, b, n, k)
<< endl;
return 0;
}

```

Output :

```25
```

This article is contributed by Abhishek Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.6 Average Difficulty : 3.6/5.0
Based on 12 vote(s)