Minimize the Sum of all the subarrays made up of the products of same-indexed elements
Last Updated :
05 Oct, 2022
Given two arrays arr[] and arr2[] of length N, the task is to find the minimum sum of all the subarrays made up of the products of the same indexed elements of both the arrays after rearranging the second array.
Note: Since the answer can be very large, print the answer modulo 109 + 7.
Examples:
Input: arr[] = {1, 2}, arr2[] = {2, 3}
Output: 14
Explanation:
Rearrange the arr2[] to {3, 2}
Therefore, the product of same indexed elements of two arrays becomes {3, 4}.
Possible subarrays are {3}, {4}, {3, 4}
Sum of the subarrays = 3 + 4 + 7 = 14.
Input: arr[] = {1, 2, 3}, arr2[] = {2, 3, 2}
Output: 43
Explanation:
Rearrange arr2[] to {3, 2, 2}
Therefore, the product of thesame indexed elements of two arrays becomes {3, 4, 6}.
Therefore, sum of all the subarrays = 3 + 4 + 6 + 7 + 10 + 13 = 43
Approach:
It can be observed that, ith element occurs in (i + 1)*(n – i) subarrays. Therefore, the task is to maximize the sum of the (i + 1)*(n – i)* a[i] * b[i]. Follow the steps below to solve the problem:
- Since the elements of arr2[] can only be rearranged, so the value of (i + 1)*(n – i) * a[i] is constant for every ith element.
- Therefore, calculate the value of the (i + 1)*(n – i)* a[i] for all the indices and then sort the products.
- Sort the array arr2[] in descending order.
- For every ith index, calculate the sum of the product of the values of (i + 1)*(n – i)* a[i] in descending order and arr2[i] in ascending order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = ( int )1e9 + 7;
bool comp(ll a, ll b)
{
if (a > b)
return true ;
else
return false ;
}
ll findMinValue(vector<ll>& a, vector<ll>& b)
{
int n = a.size();
vector<ll> pro(n);
for ( int i = 0; i < n; ++i) {
pro[i] = ((ll)(i + 1) * (ll)(n - i));
pro[i] *= (1LL * a[i]);
;
}
sort(b.begin(), b.end(), comp);
sort(pro.begin(), pro.end());
ll ans = 0;
for ( int i = 0; i < n; ++i) {
ans += (pro[i] % mod * b[i]) % mod;
ans %= mod;
}
return ans;
}
int main()
{
vector<ll> a = { 1, 2, 3 };
vector<ll> b = { 2, 3, 2 };
cout << findMinValue(a, b) << endl;
}
|
Java
import java.util.*;
class GFG{
static int mod = ( int )1e9 + 7 ;
static int findMinValue( int [] a, int []b)
{
int n = a.length;
int [] pro = new int [n];
for ( int i = 0 ; i < n; ++i)
{
pro[i] = ((i + 1 ) * (n - i));
pro[i] *= (1L * a[i]);
;
}
Integer[] input = Arrays.stream(b).boxed(
).toArray(Integer[]:: new );
Arrays.sort(input, (x, y) -> y - x);
b = Arrays.stream(input).mapToInt(
Integer::intValue).toArray();
Arrays.sort(pro);
int ans = 0 ;
for ( int i = 0 ; i < n; ++i)
{
ans += (pro[i] % mod * b[i]) % mod;
ans %= mod;
}
return ans;
}
public static void main(String[] args)
{
int []a = { 1 , 2 , 3 };
int []b = { 2 , 3 , 2 };
System.out.print(findMinValue(a, b) + "\n" );
}
}
|
Python3
mod = 1e9 + 7
def findMinValue(a, b):
n = len (a)
pro = [ 0 ] * (n)
for i in range (n):
pro[i] = ((i + 1 ) * (n - i))
pro[i] * = (a[i])
b.sort(reverse = True )
pro.sort()
ans = 0
for i in range (n):
ans + = (pro[i] % mod * b[i]) % mod
ans % = mod
return ans
if __name__ = = "__main__" :
a = [ 1 , 2 , 3 ]
b = [ 2 , 3 , 2 ]
print ( int (findMinValue(a, b)))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static int mod = ( int )1e9 + 7;
static int findMinValue( int [] a, int [] b)
{
int n = a.Length;
int [] pro = new int [n];
for ( int i = 0; i < n; ++i) {
pro[i] = ((i + 1) * (n - i));
pro[i] *= (a[i]);
;
}
List< int > input = new List< int >(b);
input = input.OrderBy(inp => -inp).ToList();
b = input.ToArray();
Array.Sort(pro);
int ans = 0;
for ( int i = 0; i < n; ++i) {
ans += (pro[i] % mod * b[i]) % mod;
ans %= mod;
}
return ans;
}
public static void Main( string [] args)
{
int [] a = { 1, 2, 3 };
int [] b = { 2, 3, 2 };
Console.WriteLine(findMinValue(a, b) + "\n" );
}
}
|
Javascript
<script>
var mod = 1000000007;
function findMinValue(a, b)
{
var n = a.length;
var pro = Array(n);
for ( var i = 0; i < n; ++i)
{
pro[i] = ((i + 1) * (n - i));
pro[i] *= (1 * a[i]);
;
}
b.sort((a, b) => b - a)
pro.sort((a, b) => a - b)
var ans = 0;
for ( var i = 0; i < n; ++i)
{
ans += (pro[i] % mod * b[i]) % mod;
ans %= mod;
}
return ans;
}
var a = [ 1, 2, 3 ];
var b = [ 2, 3, 2 ];
document.write( findMinValue(a, b));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...