1913. Maximum Product Difference Between Two Pairs - Explanation
Description
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.
You are given an integer array nums, choose four distinct indices w, x, y, and zsuch 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: 34Explanation: 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: 64Explanation: 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 <= 10,0001 <= nums[i] <= 10,000
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Sorting - One approach sorts the array to easily access the two largest and two smallest elements
- Single-Pass Min/Max Tracking - The optimal solution tracks the top 2 maximum and minimum values in one pass
- Basic Array Manipulation - Understanding how to iterate and compare elements efficiently
1. Brute Force
Intuition
The product difference is (nums[a] * nums[b]) - (nums[c] * nums[d]) where all four indices are distinct. To maximize this, we want the first product as large as possible and the second product as small as possible. The brute force approach tries all valid combinations of four distinct indices.
Algorithm
- Use four nested loops to select indices
a,b,c,d. - Skip any iteration where indices overlap.
- For each valid combination, compute the product difference.
- Track and return the maximum difference found.
class Solution:
def maxProductDifference(self, nums: List[int]) -> int:
n, res = len(nums), 0
for a in range(n):
for b in range(n):
if a == b: continue
for c in range(n):
if a == c or b == c: continue
for d in range(n):
if a == d or b == d or c == d: continue
res = max(res, nums[a] * nums[b] - nums[c] * nums[d])
return resTime & Space Complexity
- Time complexity:
- Space complexity:
2. Sorting
Intuition
To maximize the product difference, we need the two largest numbers for the first product and the two smallest numbers for the second product. After sorting, these are simply the last two and first two elements.
Algorithm
- Sort the array in ascending order.
- The maximum product is
nums[n-1] * nums[n-2](two largest). - The minimum product is
nums[0] * nums[1](two smallest). - Return their difference.
Time & Space Complexity
- Time complexity:
- Space complexity: or depending on the sorting algorithm.
3. Two Maximums and Two Minimums
Intuition
We only need the two largest and two smallest values, so we can find them in a single pass without sorting the entire array. By tracking these four values as we iterate, we achieve linear time complexity.
Algorithm
- Initialize
max1,max2to0(or the smallest possible values) andmin1,min2to infinity (or the largest possible values). - For each number in the array:
- Update
max1andmax2if the current number is among the two largest seen so far. - Update
min1andmin2if the current number is among the two smallest seen so far.
- Update
- Return
(max1 * max2) - (min1 * min2).
class Solution:
def maxProductDifference(self, nums: List[int]) -> int:
max1 = max2 = 0
min1 = min2 = float('inf')
for num in nums:
if num > max1:
max1, max2 = num, max1
elif num > max2:
max2 = num
if num < min1:
min1, min2 = num, min1
elif num < min2:
min2 = num
return (max1 * max2) - (min1 * min2)Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Forgetting to Track Both Maximums and Minimums Separately
When updating the two largest values, failing to properly shift max1 to max2 before assigning the new max1 causes the second maximum to be lost. Similarly for minimums. Always save the old value before overwriting.
Using Incorrect Initial Values
Initializing min1 and min2 to 0 instead of infinity (or the maximum possible value) leads to incorrect results when all array elements are positive. Since the problem guarantees positive integers, initialize minimums to a large value like INT_MAX or float('inf').
Sign in to join the discussion