Q1913 Accepted C++ ✅ O(n) without Sorting | Easiest & Simple
CRUX\n1) We calculate max 2 elements and min 2 elements.\n2) max1max2 -min1min2 is the answer\n3) It could be done by sorting but the time complexity would be (
CRUX\n1) We calculate max 2 elements and min 2 elements.\n2) *max1max2 -min1min2* is the answer\n3) It could be done by sorting but the time complexity would be (n*logn)\n```\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int max1=INT_MIN,max2=INT_MIN;\n        int min1=INT_MAX,min2=INT_MAX;\n        int n=nums.size();\n        for(int i=0;i<n;i++){\n            if(nums[i]>max1){ // Largest Element\n                max2=max1;\n                max1=nums[i];\n            }\n            else if(nums[i]>max2) // 2nd Larget Element\n                max2=nums[i];\n            if(nums[i]<min1){ // Smallest Element\n                min2=min1;\n                min1=nums[i];\n            }\n            else if(nums[i]<min2) // 2nd Smallest Element\n                min2=nums[i];\n        }\n        return max1*max2-min1*min2;\n    }\n};\n```
['Array', 'C']