c++ || partial_sort() || sort || traverse || nth_element()
1.TC:O(n)\n\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int firstMin=INT_MAX,secondMin=INT_MAX,firstMax=INT_MIN,secon
**1.TC:O(n)**\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int firstMin=INT_MAX,secondMin=INT_MAX,firstMax=INT_MIN,secondMax=INT_MIN;\n        for(int i=0;i<nums.size();i++){\n            if(nums[i]>firstMax){\n                secondMax=firstMax;\n                firstMax=nums[i];\n            }else if(nums[i]>secondMax){\n                secondMax=nums[i];\n            }\n            if(nums[i]<firstMin){\n                secondMin=firstMin;\n                firstMin=nums[i];\n            }else if(nums[i]<secondMin)\n                secondMin=nums[i];\n        }\n        return (firstMax*secondMax-firstMin*secondMin);\n    }\n};\n```\n**2.TC:O(nlogn)**\n```\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**3.TC:O(nlog2)**\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int n=nums.size();\n        partial_sort(nums.begin(),nums.begin()+2,nums.end());\n        int firstmin=nums[0],secmin=nums[1];\n        partial_sort(nums.begin(),nums.begin()+2,nums.end(),greater<int>());\n        int firstmax=nums[0],secmax=nums[1];\n        return firstmax*secmax - firstmin*secmin;\n    }\n};\n```\n**4.TC:O(n)**\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int n=nums.size();\n        nth_element(nums.begin(), nums.begin()+1, nums.end());\n        int firstmin=nums[0],secmin=nums[1];\n        nth_element(nums.begin(), nums.begin()+1, nums.end(),greater<int>());\n        int firstmax=nums[0],secmax=nums[1];\n        return firstmax*secmax - firstmin*secmin;\n    }\n};\n```\n**Feel free to ask any question in the comment section.**\nI hope that you\'ve found the solution useful.\nIn that case, **please do upvote and encourage me** to on my quest to document all leetcode problems\uD83D\uDE03\nHappy Coding :)\n
['C', 'Sorting', 'Interactive', 'C++']