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.

&nbsp;
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.


&nbsp;
Constraints:


	4 &lt;= nums.length &lt;= 104
	1 &lt;= nums[i] &lt;= 104

class Solution:
    def maxProductDifference(self, nums: List[int]) -> int:
        max_1 = 0
        max_2 = 0
        min_1 = 10001
        min_2 = 10001
        for i in nums:
            if i >= max_1:
                max_2,max_1 = max_1,i
            elif i > max_2:
                max_2 = i
            if i <= min_1:
                min_2,min_1 = min_1,i
            elif i < min_2:
                min_2 = i
            
        return max_1*max_2 - min_1*min_2
class Solution {
    public int maxProductDifference(int[] nums) {
        int max1 = Integer.MIN_VALUE;
        int max2 = max1;

        int min1 = Integer.MAX_VALUE;
        int min2 = min1;
        for (int i = 0; i < nums.length; i++) {
            if (max1 < nums[i]) {
                max2 = max1;
                max1 = nums[i];
            } else if(nums[i] > max2)
                max2 = nums[i];

            if(min1 > nums[i]){
                min2 = min1;
                min1 = nums[i];
            }else if (nums[i] < min2)
                min2 = nums[i];
        }
        
        return (max1 * max2) - (min1 * min2);
    }
}
class Solution {
public:
    int maxProductDifference(vector<int>& nums) {
        //we have to return the result of
        // (firstMax*secondMax) - (firstMin*secondMin)
        int max1=INT_MIN;
        int max2=INT_MIN;
        int min1=INT_MAX;
        int min2=INT_MAX;
        for(int i=0;i<nums.size();i++)
        {

            if(nums[i]>max1)
            {
                //assign the second max to max2
                max2=max1;
                max1=nums[i];
            }
            else if(nums[i]>max2)
            {
                //it can become second max
                max2= nums[i];
            }

            //check for the minimum
            if(nums[i]<min1)
            {
                //it can become first minimum
                min2=min1;
                min1=nums[i];
            }
            else if(nums[i]<min2)
            {
                //it can become second minimum
                min2=nums[i];
            }
        }
        return (max1*max2)- (min1*min2);
    }
};
var maxProductDifference = function(nums) {
    nums.sort((a,b) => a-b);
    return nums.slice(nums.length - 2).reduce((a,b) => a*b, 1) - nums.slice(0, 2).reduce((a,b) => a*b, 1);
};
Maximum Product Difference Between Two Pairs