Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Math
Maximum Product Difference Between Two Pairs

Find the maximum value of (a×b)(c×d)(a \times b) - (c \times d) by choosing four distinct numbers from the array.

Acceptance 0%
Problem Statement

Problem

You are given an integer array nums with at least four elements.

Choose four distinct indices i, j, k, l such that the expression

(nums[i]×nums[j])(nums[k]×nums[l])(nums[i] \times nums[j]) - (nums[k] \times nums[l])

is as large as possible.

Return the maximum possible value.

In other words, pick two numbers to form the first product and two different numbers to form the second product, then maximize the difference between those two products.

Notes

  • The four chosen indices must be distinct.
  • The order of the chosen pairs does not matter, only the resulting difference.
  • The array may contain duplicate values and any positive integers supported by the input type.

Input Format

  • An integer array nums with n >= 4.
  • Each element is an integer.

Output Format

  • Return a single integer: the maximum possible product difference.

Constraints

  • 4n4 \le n
  • Indices used in the two pairs must be distinct.
  • Values are assumed to fit in the platform's integer range.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [5,6,2,7,4]

Output

34

Explanation

Use the largest two numbers 7 and 6 for the first product, and the smallest two numbers 2 and 4 for the second product.

7×62×4=428=347 \times 6 - 2 \times 4 = 42 - 8 = 34.

Example 2

Input

nums = [4,2,5,9,7,4,8]

Output

64

Explanation

Choose 9 and 8 for the first product, and 2 and 4 for the second product.

9×82×4=728=649 \times 8 - 2 \times 4 = 72 - 8 = 64.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.