The product difference between two pairs (a, b) and (c, d) is (a * b) - (c * d). Given an integer array nums, choose four distinct indices w, x, y and z so that the product difference between (nums[w], nums[x]) and (nums[y], nums[z]) is as large as possible, and return that maximum.
The product difference between two pairs (a, b) and (c, d) is (a * b) - (c * d). Given an integer array nums, choose four distinct indices w, x, y and z so that the product difference between (nums[w], nums[x]) and (nums[y], nums[z]) is as large as possible, and return that maximum.
Example 1:
Input: {"nums":[5,6,2,7,4]}
Output: 34
Constraints:
4≤nums.length≤104
1≤nums[i]≤104
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[5,6,2,7,4]}
The best difference uses the two largest numbers for the first product and the two smallest for the second. Track both pairs in one pass.