{x}
blog image

Minimize Product Sum of Two Arrays

Solution Explanation: Minimize Product Sum of Two Arrays

This problem asks to find the minimum product sum of two arrays, nums1 and nums2, after rearranging the elements of nums1. The product sum is defined as the sum of the element-wise products of the two arrays (i.e., nums1[0]*nums2[0] + nums1[1]*nums2[1] + ...).

Optimal Approach: Greedy Algorithm with Sorting

The key insight is that to minimize the product sum, we should pair the smallest elements of one array with the largest elements of the other array, and vice versa. This is a greedy approach.

  1. Sort: We sort nums1 in ascending order and nums2 in descending order. This ensures that the smallest elements of nums1 are paired with the largest elements of nums2, and so on.

  2. Calculate Product Sum: We then iterate through the sorted arrays and calculate the sum of the element-wise products.

Time Complexity Analysis:

The dominant operations are the sorting steps. Sorting algorithms generally have a time complexity of O(n log n), where n is the length of the array. Since we sort two arrays of length n, the overall time complexity is O(n log n).

Space Complexity Analysis:

The space complexity depends on the sorting algorithm used. In-place sorting algorithms (like optimized versions of quicksort or mergesort) have a space complexity of O(log n) due to the recursive call stack. Other sorting algorithms might require O(n) extra space. Therefore, the space complexity is O(log n) or O(n) depending on the sorting implementation.

Code Implementation (Python):

class Solution:
    def minProductSum(self, nums1: List[int], nums2: List[int]) -> int:
        nums1.sort()  #Sort nums1 in ascending order
        nums2.sort(reverse=True) #Sort nums2 in descending order
        product_sum = 0
        for i in range(len(nums1)):
            product_sum += nums1[i] * nums2[i]
        return product_sum
 

Code Implementation (Java):

import java.util.Arrays;
 
class Solution {
    public int minProductSum(int[] nums1, int[] nums2) {
        Arrays.sort(nums1); //Sort nums1 in ascending order
        Arrays.sort(nums2); //Sort nums2 in descending order
        int productSum = 0;
        for (int i = 0; i < nums1.length; i++) {
            productSum += nums1[i] * nums2[nums1.length - 1 - i]; //Pair smallest of nums1 with largest of nums2
        }
        return productSum;
    }
}

Code Implementation (C++):

#include <algorithm>
#include <vector>
 
class Solution {
public:
    int minProductSum(std::vector<int>& nums1, std::vector<int>& nums2) {
        std::sort(nums1.begin(), nums1.end()); //Sort nums1 in ascending order
        std::sort(nums2.begin(), nums2.end(), std::greater<int>()); //Sort nums2 in descending order
        int productSum = 0;
        for (size_t i = 0; i < nums1.size(); ++i) {
            productSum += nums1[i] * nums2[i]; //Pair smallest of nums1 with largest of nums2
        }
        return productSum;
    }
};

The code implementations in other languages (Go, TypeScript etc.) would follow a very similar structure, utilizing the built-in sorting functions of those respective languages. The core logic of sorting and calculating the element-wise product sum remains consistent across all implementations.