Minimize the sum of product of two arrays with permutations allowed
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]. Shuffling of elements of arrays A and B is allowed.
Examples :
Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}.
Output : 23
Minimum value of S = 1*6 + 1*5 + 3*4 = 23.
Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 }
Output : 80.
Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80.
The idea is to multiply minimum element of one array to maximum element of another array. Algorithm to solve this problem:
- Sort both the arrays A and B.
- Traverse the array and for each element, multiply A[i] and B[n – i – 1] and add to the total.
Note: We are adding multiplication of elements which can lead to overflow conditions.
Below image is an illustration of the above approach:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int minValue( int A[], int B[], int n)
{
sort(A, A + n);
sort(B, B + n);
long long int result = 0;
for ( int i = 0; i < n; i++)
result += (A[i] * B[n - i - 1]);
return result;
}
int main()
{
int A[] = { 3, 1, 1 };
int B[] = { 6, 5, 4 };
int n = sizeof (A) / sizeof (A[0]);
cout << minValue(A, B, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static long minValue( int A[], int B[], int n)
{
Arrays.sort(A);
Arrays.sort(B);
long result = 0 ;
for ( int i = 0 ; i < n; i++)
result += (A[i] * B[n - i - 1 ]);
return result;
}
public static void main(String[] args)
{
int A[] = { 3 , 1 , 1 };
int B[] = { 6 , 5 , 4 };
int n = A.length;
;
System.out.println(minValue(A, B, n));
}
}
|
Python3
def minValue(A, B, n):
A.sort()
B.sort()
result = 0
for i in range (n):
result + = (A[i] * B[n - i - 1 ])
return result
A = [ 3 , 1 , 1 ]
B = [ 6 , 5 , 4 ]
n = len (A)
print (minValue(A, B, n))
|
C#
using System;
class GFG {
static long minValue( int [] a, int [] b,
int n)
{
Array.Sort(a);
Array.Sort(b);
long result = 0;
for ( int i = 0; i < n; i++)
result += (a[i] * b[n - i - 1]);
return result;
}
public static void Main()
{
int [] a = { 3, 1, 1 };
int [] b = { 6, 5, 4 };
int n = a.Length;
Console.Write(minValue(a, b, n));
}
}
|
PHP
<?php
function minValue( $A , $B , $n )
{
sort( $A ); sort( $A , $n );
sort( $B ); sort( $B , $n );
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
$result += ( $A [ $i ] *
$B [ $n - $i - 1]);
return $result ;
}
$A = array ( 3, 1, 1 );
$B = array ( 6, 5, 4 );
$n = sizeof( $A ) / sizeof( $A [0]);
echo minValue( $A , $B , $n ) ;
?>
|
Javascript
<script>
function minValue(A, B, n)
{
A.sort( function (a,b){
return a - b;
});
B.sort( function (a,b){
return a - b;
});
let result = 0;
for (let i = 0; i < n; i++)
result += (A[i] * B[n - i - 1]);
return result;
}
let A = [ 3, 1, 1 ];
let B = [ 6, 5, 4 ];
let n = A.length;
document.write(minValue(A, B, n));
</script>
|
Time Complexity: O(n log n).
Auxiliary Space: O(1) because it is using constant space for variables
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...