Skip to main content
Back to problems
#1913
Easy Algorithms

Maximum product difference between two pairs

Array Sorting
83.0% acceptance
Feb 25, 2026
1584
69
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.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_product_difference(mut nums: Vec<i32>) -> i32 {
    nums.sort();
    let n = nums.len();
    nums[n - 1] * nums[n - 2] - nums[0] * nums[1]
  }
}