🔥 Beats 100% | 2 Approaches | Easiest code with explaination 🚀😎
Intuition\r\nThe goal is to maximize the product difference between two pairs. To achieve this, we want to select the two largest numbers for the first pair and
# Intuition\r\nThe goal is to maximize the product difference between two pairs. To achieve this, we want to select the two largest numbers for the first pair and the two smallest numbers for the second pair. By sorting the array and choosing the extremes, we can maximize the product difference.\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\n1. First calculate the maximum number store it and then erase it.\r\n2. Similarly, we need one more number follow the same procedure.\r\n3. For minimum numbers also steps are similar.\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity: O(N)\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity: O(1)\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\n// 1. Using Sorting\r\nclass Solution {\r\n\r\npublic:\r\n\r\n    int maxProductDifference(vector<int>& nums) {\r\n\r\n        ios_base::sync_with_stdio(false);\r\n\r\n        cout.tie(0);\r\n\r\n        cin.tie(0);\r\n\r\n        sort(nums.begin(),nums.end());\r\n\r\n        int n = nums.size();\r\n\r\n        return ((nums[n-1] * nums[n-2]) - (nums[0] * nums[1]));\r\n\r\n    }\r\n\r\n};\r\n\r\n// 2. Using STL\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n        ios_base::sync_with_stdio(false);\r\n        cout.tie(0);\r\n        cin.tie(0);\r\n        \r\n        // code\r\n        auto it = max_element(nums.begin(),nums.end());\r\n        int index = it - nums.begin();\r\n        int max1 = nums[index];\r\n        nums.erase(nums.begin()+index);\r\n\r\n        it = max_element(nums.begin(),nums.end());\r\n        index = it - nums.begin();\r\n        int max2 = nums[index];\r\n        nums.erase(nums.begin()+index);\r\n        cout << max1 << " " << max2 << endl;\r\n\r\n        it = min_element(nums.begin(),nums.end());\r\n        index = it - nums.begin();\r\n        int min1 = nums[index];\r\n        nums.erase(nums.begin()+index);\r\n\r\n        it = min_element(nums.begin(),nums.end());\r\n        index = it - nums.begin();\r\n        int min2 = nums[index];\r\n        nums.erase(nums.begin()+index);\r\n        \r\n        return (max1 * max2 - min1 * min2);\r\n    }\r\n};\r\n```
['Array', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']