Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).
(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 <= 1041 <= nums[i] <= 104Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[5,6,2,7,4]
[4,2,5,9,7,4,8]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1913: Maximum Product Difference Between Two Pairs
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];
}
}
// Accepted solution for LeetCode #1913: Maximum Product Difference Between Two Pairs
func maxProductDifference(nums []int) int {
sort.Ints(nums)
n := len(nums)
return nums[n-1]*nums[n-2] - nums[0]*nums[1]
}
# Accepted solution for LeetCode #1913: Maximum Product Difference Between Two Pairs
class Solution:
def maxProductDifference(self, nums: List[int]) -> int:
nums.sort()
return nums[-1] * nums[-2] - nums[0] * nums[1]
// Accepted solution for LeetCode #1913: Maximum Product Difference Between Two Pairs
struct Solution;
impl Solution {
fn max_product_difference(mut nums: Vec<i32>) -> i32 {
let n = nums.len();
nums.sort_unstable();
nums[n - 1] * nums[n - 2] - nums[0] * nums[1]
}
}
#[test]
fn test() {
let nums = vec![5, 6, 2, 7, 4];
let res = 34;
assert_eq!(Solution::max_product_difference(nums), res);
let nums = vec![4, 2, 5, 9, 7, 4, 8];
let res = 64;
assert_eq!(Solution::max_product_difference(nums), res);
}
// Accepted solution for LeetCode #1913: Maximum Product Difference Between Two Pairs
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1913: Maximum Product Difference Between Two Pairs
// 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];
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.