Maximum Product Difference Between Two Pairs -> Optimal Solution (Time Complexity -O(n))
Methods\r\n Describe your first thoughts on how to solve this problem.  This Problem can be solved using Two logics one based on Sorting and another one is base
# Methods\r\n<!-- Describe your first thoughts on how to solve this problem. --> This Problem can be solved using Two logics one based on Sorting and another one is based on taking largest and second largest as well as smallest and second smallest.\r\n # Approach\r\n<!-- Describe your approach to solving the problem. -->\r\nHere we have the approach of the second logic which has $$O(n)$$ Time Complexity.\r\nLet\'s declare four integer values `mini`(smallest) , `smini`(second smallest) , `maxi`(largest) , `smaxi`(second largest).\r\n`mini` and `maxi` values can be taken using inbuilt functions. Then find there occcurenece if they are unique , these elements will get neglected while `smaxi` & `smini` calculation in the for loop and if there frequenecy is more than one , then we can also count as the second largest and second smallest accordingly. In the final step do that calulation $$(maxi*smaxi) - (mini*smini)$$.\r\n\r\n# Complexity\r\n- Time complexity: $$O(n)$$\r\n\r\n- Space complexity: $$O(n)$$\r\n\r\n# If you like this solution pls upvote this \uD83D\uDE03\r\n\r\n# C++ Code\r\n```\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n        int n = nums.size();\r\n        int mini = INT_MAX , smini = mini , maxi = INT_MIN, smaxi = maxi;\r\n        mini = *min_element(nums.begin(),nums.end());\r\n        maxi = *max_element(nums.begin(),nums.end());\r\n      unordered_map<int,int>mp;// To check the number of occurrence of maxi & mini\r\n        for(auto it : nums)mp[it]++;\r\n        for(int i=0;i<n;i++){\r\n            if((nums[i]==maxi && mp[maxi]==1) ||(nums[i]==mini && mp[mini]==1))continue;\r\n            smini = min(smini,nums[i]);\r\n            smaxi = max(smaxi,nums[i]);\r\n        }\r\n        int res = (maxi*smaxi)-(mini*smini);\r\n        return res;\r\n    }\r\n};\r\n```
['C++']