Beats 99% || [Java/C++/Python3] || Beginner Friendly Solution
Intuition\n1. Product difference  will be maximum when we will, subtract of two smallest number from the product of two greatest number.\n2. We have to find the
# Intuition\n1. Product difference  will be maximum when we will, subtract of two smallest number from the product of two greatest number.\n2. We have to find the greatest two number and smallest two number.\n\n# Approach\n1. **Initialization:**\n    - Set `min1` to the first element in the array (nums[0]).\n    - Set `min2` to the second element in the array (nums[1]).\n    - Set `max1` to the larger of the first two elements.\n    - Set `max2` to the smaller of the first two elements.\n2. Iterative Update:\n    - Iterate through the array starting from the third element (`i=2`).\n    - Update `min1` and `min2` if the current element is smaller than both `min1` and `min2`.\n    - Update `max1` and `max2` if the current element is larger than both `max1` and `max2`.\n3. **Calculation of Result:**\n    - After the iteration, calculate the result by subtracting the product of the two smallest elements (`min1` `*` `min2`) from the product of the two largest elements (`max1` `*` `max2`).\n    \n*The code essentially identifies the two smallest and two largest elements in the array and computes the maximum product difference using these values. It\'s an efficient way to find the desired result in a single pass through the array.*\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` Java []\nclass Solution {\n    public int maxProductDifference(int[] nums) {\n        if(nums.length <= 2) {\n            return 0;\n        }\n\n        int min1 = nums[0]; //Smallest\n        int min2 = nums[1]; //Second Smallest\n        int max1 = nums[1]; //Largest\n        int max2 = nums[0]; //Second Largest\n        if(nums[0] > nums[1]){\n            min1 = nums[1];\n            min2 = nums[0];\n            max1 = nums[0];\n            max2 = nums[1];\n        }\n\n        for(int i=2; i<nums.length; i++) {\n            if(nums[i] < min2) {\n                if(nums[i] < min1) {\n                    min2 = min1;\n                    min1 = nums[i];\n                }\n                else {\n                    min2 = nums[i];\n                }\n            }\n\n            if(nums[i] > max2) {\n                if(nums[i] > max1) {\n                    max2 = max1;\n                    max1 = nums[i];\n                }\n                else {\n                    max2 = nums[i];\n                }\n            }\n        }\n        return max1*max2 - min1*min2;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        if (nums.size() <= 2) {\n            return 0;\n        }\n\n        int min1 = nums[0]; // Smallest\n        int min2 = nums[1]; // Second Smallest\n        int max1 = nums[1]; // Largest\n        int max2 = nums[0]; // Second Largest\n        if (nums[0] > nums[1]) {\n            min1 = nums[1];\n            min2 = nums[0];\n            max1 = nums[0];\n            max2 = nums[1];\n        }\n\n        for (int i = 2; i < nums.size(); i++) {\n            if (nums[i] < min2) {\n                if (nums[i] < min1) {\n                    min2 = min1;\n                    min1 = nums[i];\n                } else {\n                    min2 = nums[i];\n                }\n            }\n\n            if (nums[i] > max2) {\n                if (nums[i] > max1) {\n                    max2 = max1;\n                    max1 = nums[i];\n                } else {\n                    max2 = nums[i];\n                }\n            }\n        }\n        return max1 * max2 - min1 * min2;\n    }\n};\n```\n``` Python3 []\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        if len(nums) <= 2:\n            return 0\n\n        min1 = min(nums[0], nums[1])  # Smallest\n        min2 = max(nums[0], nums[1])  # Second Smallest\n        max1 = max(nums[0], nums[1])  # Largest\n        max2 = min(nums[0], nums[1])  # Second Largest\n\n        for i in range(2, len(nums)):\n            if nums[i] < min2:\n                if nums[i] < min1:\n                    min2 = min1\n                    min1 = nums[i]\n                else:\n                    min2 = nums[i]\n\n            if nums[i] > max2:\n                if nums[i] > max1:\n                    max2 = max1\n                    max1 = nums[i]\n                else:\n                    max2 = nums[i]\n\n        return max1 * max2 - min1 * min2\n```
['Math', 'C++', 'Java', 'Python3']