Find the maximum value of by choosing four distinct numbers from the array.
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
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
numswithn >= 4. - Each element is an integer.
Output Format
- Return a single integer: the maximum possible product difference.
Constraints
- Indices used in the two pairs must be distinct.
- Values are assumed to fit in the platform's integer range.
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.
.
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.
.
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.