2️⃣ Approach Explained ✅ || C++ solution || Easy to understand 👍
\u27A1\uFE0F Approach 1: Brute Force\n# Intuition\n Describe your first thoughts on how to solve this problem. \narranging elements in increasing order . \n# Ap
# \u27A1\uFE0F Approach 1: Brute Force\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\narranging elements in increasing order . \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. sort the given array.\n2. return absolute value of `firstEle*SecondEle` - `secondLastEle*LastEle`.\n\n# Complexity\n- Time complexity:$$O(nlogn)$$\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```C++ []\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int n=nums.size();\n        return nums[n-1]*nums[n-2] - nums[0]*nums[1];\n    }\n};\n```\n```python []\nclass Solution:\n    def maxProductDifference(self, nums):\n        nums.sort()\n        n = len(nums)\n        return nums[n-1] * nums[n-2] - nums[0] * nums[1]\n```\n\n\n\n# \u27A1\uFE0F Approach 2 : Single Pass\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nfinding maximun ,second maximun,minimum and second minimun.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize four variables: `maxi`, `secMaxi`, `mini`, and `secMini` to handle the maximum and minimum values.\n2. Iterate through the input vector `(nums)`.\n    1. For each element, update the maximum values `(maxi and secMaxi)` if the current element is greater than or equal to `maxi` or falls between `secMaxi` and `maxi`.\n    2. Update the minimum values `(mini and secMini)` if the current element is less than or equal to mini or falls between `mini` and `secMini`.\n3. Calculate the maximum product difference using the formula: `(maxi * secMaxi) - (mini * secMini)`.\n4. Return the calculated result.\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```C++ []\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n       int maxi=INT_MIN;\n       int secMaxi=INT_MIN;\n       int mini=INT_MAX;\n       int secMini=INT_MAX;\n\n       for(int i=0;i<nums.size();i++)\n       {\n           if(nums[i]>= maxi)\n           {\n               secMaxi=maxi;\n               maxi=nums[i];\n           }\n           else if(secMaxi < nums[i] && nums[i] < maxi)\n           {\n               secMaxi=nums[i];\n           }\n\n\n           if(nums[i]<=mini)\n           {\n               secMini=mini;\n               mini=nums[i];\n           }\n           else if(secMini > nums[i] && nums[i] > mini)\n           {\n               secMini=nums[i];\n           }\n       }\n       return maxi*secMaxi - mini*secMini;\n    }\n};\n```\n```python []\nclass Solution:\n    def maxProductDifference(self, nums):\n        maxi = float(\'-inf\')\n        secMaxi = float(\'-inf\')\n        mini = float(\'inf\')\n        secMini = float(\'inf\')\n\n        for num in nums:\n            if num >= maxi:\n                secMaxi = maxi\n                maxi = num\n            elif secMaxi < num < maxi:\n                secMaxi = num\n\n            if num <= mini:\n                secMini = mini\n                mini = num\n            elif secMini > num > mini:\n                secMini = num\n\n        return (maxi * secMaxi) - (mini * secMini)\n```\n\n![upvote.jpg](https://assets.leetcode.com/users/images/bd378ba7-7ce3-4d36-b371-d0c2e052e1b5_1702887217.4287694.jpeg)\n
['Array', 'Sorting', 'Python', 'C++']