Minimize the sum of product | Practice | GeeksforGeeks

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.


Example 1:
Input:
N = 3 
A[] = {3, 1, 1}
B[] = {6, 5, 4}
Output:
23 
Explanation:
1*6+1*5+3*4 = 6+5+12
= 23 is the minimum sum
 
Example 2:
Input:
N = 5
A[] = {6, 1, 9, 5, 4}
B[] = {3, 4, 8, 2, 4}
Output:
80
Explanation:
2*9+3*6+4*5+4*4+8*1
=18+18+20+16+8
= 80 is the minimum sum
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minValue() which takes the arrays A[], B[] and its size N as inputs and returns the minimum sum.
 

Expected Time Complexity: O(N. log(N))
Expected Auxiliary Space: O(1)


Constraints:
1 ≤ N ≤ 105
1 ≤ A[] ≤ 106
0
sakesai30in 10 hours
class Solution {
   public long minValue(long a[], long b[], long n)    {       int m = (int)n; Arrays.parallelSort(a); Arrays.parallelSort(b); long prod = 0; for(int i =0;i<m;i++) {  prod = prod+(a[i]*b[m-i-1]); } return prod;   }}
+1
diwakarghosh410 hours ago
Python Code:
class Solution:   def minValue(self, a, b, n):       # Your code goes here       sump = 0       a.sort()       b.sort(reverse = True)       for i in range(0, n):           sump += a[i]*b[i]       return sump 
0
mehraparthak01 week ago
    long long int minValue(int a[], int b[], int n)   {       long long int sum=0;       sort(a,a+n);       sort(b,b+n);       for(long long int i=0,j=n-1;i<n,j>=0;i++,j--){           long long int num=(a[i]*b[j]);           sum+=num;       }       return sum;   }
0
mehraparthak01 week ago
    long long int minValue(int a[], int b[], int n)   {       long long int sum=0;       sort(a,a+n);       sort(b,b+n,greater<int>());       for(long long int i=0,j=n-1;i<n;i++){           long long int num=(a[i]*b[i]);           sum+=num;       }       return sum;   }
0
ashp63681 week ago
        res=0
        for i,j in zip(sorted(a),sorted(b,reverse=True)):
            res+= i*j
        return res
        #time taken : 0.84
0
vishwajeetpawar192 weeks ago
class Solution{
    public:
    long long int minValue(int a[], int b[], int n)
    {
        // Your code goes here
       
        sort(a,a+n,greater<int>()) ;
        sort(b,b+n) ;
        
        long long int sum = 0 ;
        
        
        for(int i=0;i<n;i++)
        {
            //cout<<a[i]<<"   "<<b[i]<<endl;
            sum+=a[i]*b[i] ;
        }        
        return sum ;
    }
};

+1
sailendrachettri2 weeks ago
c++ solution
  long long int minValue(int a[], int b[], int n)
    {
        sort(a, a+n);
        sort(b, b+n, greater<int>());
        
        long long min_sum = 0;
        
        for(int i = 0; i < n; i++){
            min_sum += a[i] * b[i];
        }
        return min_sum;
    }
0
yashodeep312 weeks ago
python code:→
class Solution:   def minValue(self, a, b, n):       a.sort()       b.sort()       sum=0       for i in range(0,n):           sum += a[i] * b[n-i-1]       return sum
+1
badal8march2 weeks ago
class Solution{
    public:
    long long int minValue(int a[], int b[], int n)
    {
        // Your code goes here
        sort(a,a+n);
        sort(b,b+n,greater<int>());
        long long sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=a[i]*b[i];
        }
        return sum;
        
    }
};
0
prishita3 weeks ago
JAVA Solution:
class Solution {

    public long minValue(long a[], long b[], long n) 
    {
        // Your code goes here
        Arrays.sort(a);
        Arrays.sort(b);
        long sum=0;
        for(int i=0;i<n;i++){
            sum+=a[i]*b[(int)n-1-i];
        }
        return sum;
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.