Maximum Product Difference Between Two Pairs (LeetCode 1913): Solution and Explanation
Pick two numbers to multiply and two other numbers to multiply, then subtract the second product from the first. Return the largest result possible.
Intuition
Every number is positive, so a product only grows when a factor grows. To make the first product as large as possible, use the two largest numbers in the array.
For the same reason, the product you subtract is smallest when it uses the two smallest numbers. Since the array has at least four elements, these four can always come from different positions.
So the answer is fixed once you know four values: the top two and the bottom two. You do not need to sort to find them. One pass that keeps a running top pair and bottom pair is enough.
Approach: One pass with two running pairs
- Start with
big1 = big2 = 0andsmall1 = small2 = infinity. - For each x: if x > big1, big1 slides down to big2 and x becomes big1. Otherwise, if x > big2, x replaces big2.
- Independently: if x < small1, small1 moves to small2 and x becomes small1. Otherwise, if x < small2, x replaces small2.
- Return
big1 * big2 - small1 * small2.
Step-by-step example
nums = [5,6,2,7,4]
- x = 5: top pair (5, 0), bottom pair (5, inf).
- x = 6: beats big1, so top pair (6, 5). It also fills small2: bottom pair (5, 6).
- x = 2: below small1, so bottom pair (2, 5). Top pair unchanged.
- x = 7: beats big1, so top pair (7, 6).
- x = 4: between 2 and 5, so it replaces small2: bottom pair (2, 4).
- Return 7 * 6 - 2 * 4 = 42 - 8 = 34.
Code: Python, Java, C++
1class Solution:
2 def maxProductDifference(self, nums: List[int]) -> int:
3 big1 = big2 = 0
4 small1 = small2 = float('inf')
5 for x in nums:
6 if x > big1:
7 big1, big2 = x, big1
8 elif x > big2:
9 big2 = x
10 if x < small1:
11 small1, small2 = x, small1
12 elif x < small2:
13 small2 = x
14 return big1 * big2 - small1 * small21class Solution {
2 public int maxProductDifference(int[] nums) {
3 int big1 = 0, big2 = 0;
4 int small1 = Integer.MAX_VALUE, small2 = Integer.MAX_VALUE;
5 for (int x : nums) {
6 if (x > big1) {
7 big2 = big1;
8 big1 = x;
9 } else if (x > big2) {
10 big2 = x;
11 }
12 if (x < small1) {
13 small2 = small1;
14 small1 = x;
15 } else if (x < small2) {
16 small2 = x;
17 }
18 }
19 return big1 * big2 - small1 * small2;
20 }
21}1class Solution {
2public:
3 int maxProductDifference(vector<int>& nums) {
4 int big1 = 0, big2 = 0;
5 int small1 = INT_MAX, small2 = INT_MAX;
6 for (int x : nums) {
7 if (x > big1) {
8 big2 = big1;
9 big1 = x;
10 } else if (x > big2) {
11 big2 = x;
12 }
13 if (x < small1) {
14 small2 = small1;
15 small1 = x;
16 } else if (x < small2) {
17 small2 = x;
18 }
19 }
20 return big1 * big2 - small1 * small2;
21 }
22};Time and space complexity
O(n)
Each element is compared against at most four tracked values.
O(1)
Only four numbers are kept.
Other approaches
Sort (time O(n log n), space O(1) to O(n) depending on the sort)
Sort the array and return nums[n-1] * nums[n-2] - nums[0] * nums[1]. Shorter to write, slower for large inputs.
Partial selection (time O(n), space O(1))
Use a heap or nth_element to pull out the two largest and two smallest. Works, but the four-variable pass is simpler.
Common mistakes
- Forgetting to slide the old big1 down to big2 (or small1 up to small2) when a new extreme arrives.
- Using else between the top and bottom updates. The first element, for example, belongs to both pairs.
- Worrying about overflow: the largest product is 10^4 * 10^4 = 10^8, which fits in a 32-bit int.
Frequently asked questions
Can the same element be in both pairs?
No, the four indices must be distinct. Because n is at least 4, the two largest and two smallest positions never need to overlap: with duplicates you simply use different copies.
Would the answer change with negative numbers?
Yes. Two large negatives can make a big positive product, so the greedy choice would no longer hold. The constraints guarantee every value is at least 1.
Is sorting acceptable in an interview?
Usually yes, but mention the O(n) pass as the follow-up. It is the same idea without the sort.