{x}
blog image

Maximum Product Difference Between Two Pairs

The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).

  • For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16.

Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.

Return the maximum such product difference.

 

Example 1:

Input: nums = [5,6,2,7,4]
Output: 34
Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
The product difference is (6 * 7) - (2 * 4) = 34.

Example 2:

Input: nums = [4,2,5,9,7,4,8]
Output: 64
Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
The product difference is (9 * 8) - (2 * 4) = 64.

 

Constraints:

  • 4 <= nums.length <= 104
  • 1 <= nums[i] <= 104

Solution Explanation

The problem asks to find the maximum product difference between two pairs of numbers chosen from an input array nums. The product difference is defined as (a * b) - (c * d), where (a, b) and (c, d) are the two chosen pairs. To maximize this difference, we need to maximize the product a * b and minimize the product c * d.

Approach:

The most efficient way to solve this problem involves sorting the input array. After sorting, the two largest numbers will form the pair that maximizes the product a * b, and the two smallest numbers will form the pair that minimizes the product c * d.

Algorithm:

  1. Sort: Sort the input array nums in ascending order.
  2. Calculate: Calculate the product difference as nums[n-1] * nums[n-2] - nums[0] * nums[1], where n is the length of the array. nums[n-1] and nums[n-2] are the two largest numbers, and nums[0] and nums[1] are the two smallest numbers.
  3. Return: Return the calculated product difference.

Time Complexity Analysis:

The dominant operation is sorting the array. The time complexity of most efficient sorting algorithms (like merge sort or quicksort) is O(n log n), where n is the length of the array. The calculation of the product difference takes constant time, O(1). Therefore, the overall time complexity is O(n log n).

Space Complexity Analysis:

In-place sorting algorithms can be used, leading to a space complexity of O(1) for the sorting step. The space used for storing intermediate variables is constant. Therefore, the overall space complexity is O(1) (or O(n) if you use a sorting method with extra space).

Code in Different Languages

The following code snippets implement the solution described above in various programming languages:

Python:

class Solution:
    def maxProductDifference(self, nums: List[int]) -> int:
        nums.sort()
        return nums[-1] * nums[-2] - nums[0] * nums[1]

Java:

import java.util.Arrays;
 
class Solution {
    public int maxProductDifference(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        return nums[n - 1] * nums[n - 2] - nums[0] * nums[1];
    }
}

C++:

#include <algorithm>
#include <vector>
 
class Solution {
public:
    int maxProductDifference(std::vector<int>& nums) {
        std::sort(nums.begin(), nums.end());
        int n = nums.size();
        return nums[n - 1] * nums[n - 2] - nums[0] * nums[1];
    }
};

Go:

import "sort"
 
func maxProductDifference(nums []int) int {
	sort.Ints(nums)
	n := len(nums)
	return nums[n-1]*nums[n-2] - nums[0]*nums[1]
}

JavaScript:

/**
 * @param {number[]} nums
 * @return {number}
 */
var maxProductDifference = function(nums) {
    nums.sort((a, b) => a - b);
    let n = nums.length;
    return nums[n - 1] * nums[n - 2] - nums[0] * nums[1];
};

All these solutions follow the same basic algorithm: sort the array and then perform a simple calculation to get the maximum product difference. The choice of language affects only the syntax and specific sorting functions used, but the underlying algorithmic approach remains consistent.