NeetCode #73LC-1913EasyArrays & Hashing
← Back to All Problems#73 · #1913 · Maximum Product Difference Between Two Pairs(两个数对之间的最大乘积差)
📌 Problem Statement & Constraints
The product difference between two pairs
(a, b) and (c, d) is (a * b) - (c * d). Given an array nums of distinct integers, return the maximum product difference over all choices of four distinct indices. Constraints: 4 <= nums.length <= 10^4, 1 <= nums[i] <= 10^4.💡 Core Algorithmic Approaches
- The maximum is achieved by taking the two largest values for the positive product and the two smallest for the subtracted one.
- Sort the array, then the answer is
nums[-1] * nums[-2] - nums[0] * nums[1]. - Since all values are positive, the sign logic is unambiguous -- no need to consider negative products.
- A single pass tracking the two largest and two smallest values avoids the sort entirely.
💻 Benchmark Python3 Implementation
class Solution:
def maxProductDifference(self, nums: List[int]) -> int:
nums.sort()
return nums[-1] * nums[-2] - nums[0] * nums[1]
# One-pass variant: O(n) time, O(1) space
class Solution2:
def maxProductDifference(self, nums: List[int]) -> int:
mx1 = mx2 = -1 # two largest
mn1 = mn2 = float("inf") # two smallest
for x in nums:
if x > mx1:
mx1, mx2 = x, mx1
elif x > mx2:
mx2 = x
if x < mn1:
mn1, mn2 = x, mn1
elif x < mn2:
mn2 = x
return mx1 * mx2 - mn1 * mn2⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log n) for the sort-based version, O(n) for the one-pass version.
💾 Space Complexity
O(1) for both (the sort is in place).
⚠️ Interview Pitfalls & Follow-ups
- Subtracting the two largest from the two smallest in the wrong order: the large product must come first.
- Assuming negative values exist: the constraints guarantee
nums[i] >= 1, which is what makes the simple formula valid. With negatives you would have to considermin * minfor the positive term. - Using the same index twice: the four indices must be distinct, which is automatic since the two largest and two smallest come from disjoint positions when
n >= 4.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Arrays & Hashing Problems
中文速览与面试要点
两个数对之间的最大乘积差
两个数对 (a, b) 和 (c, d) 之间的乘积差定义为 (a × b) − (c × d) 。给你一个整数数组 nums,选出四个 不同 的下标 w、x、y、z,使数对 (nums[w], nums[x]) 和 (nums[y], nums[z]) 之间的乘积差取到最大值。返回以这种方式取得的乘积差的最大值。约束:4 ≤ nums.length ≤ 10^4;1 ≤ nums[i] ≤ 10^4。
核心要点
- 排序后取极值(标准解) :要使 (a × b) − (c × d) 最大,应让第一对乘积最大、第二对乘积最小。
- 关键观察 :nums 全为正数,因此「乘积最大」对应「取最大的两个数」,「乘积最小」对应「取最小的两个数」。
- 实现 :升序排序后返回 nums[-1] × nums[-2] − nums[0] × nums[1] 。
- 若允许负数 :则「乘积最大」可能是两个最小负数之积,「乘积最小」可能是最大正数与最小负数之积,需要额外考虑两种组合。本题约束 nums[i] ≥ 1 规避了这一复杂性。
常见陷阱
- 忽略「全为正数」这一前提 :若数组含负数,直接取最大两数与最小两数会出错,需比较「最大两数积、最小两数积」与「最大最小交叉积」三种组合。本题约束规避了,但面试中被追问时要说清。
- 下标写错 :最大两数是 nums[-1] 与 nums[-2] ,最小两数是 nums[0] 与 nums[1] 。写成 nums[-1] * nums[0] 就完全错了。
- 误以为需要枚举四个下标 :C(n,4) 组合数是 O(n^4),完全不可行。极值观察把问题降到 O(n log n) 甚至 O(n)。
参考实现
class Solution:
def maxProductDifference(self, nums: list[int]) -> int:
nums.sort()
# 全为正数:最大两数之积 - 最小两数之积
return nums[-1] * nums[-2] - nums[0] * nums[1]