Beat 99.6% of users and you will find the solution very handy..
Intuition\r\nsorting is easy but try it with O(n) time complexity..it has been described below..\r\n\r\n\r\n# Approach\r\nit\'s an simple approach , just find t
# Intuition\r\nsorting is easy but try it with O(n) time complexity..it has been described below..\r\n\r\n\r\n# Approach\r\nit\'s an simple approach , just find the first largest and second largest elements and then find the first smallest and the second smallest elements. BUT wait here\'s the twist comes...\r\n * First largest and the second largest element may be same \r\n* you might find error answer if the very first element is the largest or the smallest \r\n* these are the two above points you need to deal with rest its an simple question.\r\n\r\n# Complexity\r\n- Time complexity:\r\nO(n)\r\n\r\n- Space complexity:\r\nO(n)\r\n\r\n# Code\r\n```\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n       int max2=nums[0];\r\n       int y=0;\r\n       for(int i=0;i<nums.size();i++){\r\n           if(nums[i]>max2){\r\n               max2=nums[i];\r\n               y=i;\r\n           }\r\n       }\r\n       int max1=nums[0];\r\n       if(max2==nums[0]){\r\n           max1=nums[1];\r\n       }\r\n       for(int i=0;i<nums.size();i++){\r\n           if(nums[i]>max1 && i!=y){\r\n               max1=nums[i];\r\n\r\n           }\r\n        }  \r\n        int x=0;\r\n        int min2=nums[0];\r\n        for(int i=0;i<nums.size();i++){\r\n           if(nums[i]<min2){\r\n               min2=nums[i];\r\n               x=i;\r\n           }\r\n        }\r\n        int min1=nums[0];\r\n        if(min2==nums[0]){\r\n            min1=nums[1];\r\n        }\r\n         for(int i=0;i<nums.size();i++){\r\n           if(i!=x && nums[i]<min1 ){\r\n               min1=nums[i];\r\n\r\n           }\r\n        }  \r\n        \r\n\r\n      return (max1*max2)-(min1*min2);\r\n    }\r\n};\r\n```
['C++']