Easy CPP approach||beats 99% users||19ms||100%working
PLSSS UPVOTE\uD83E\uDD79\uD83E\uDD79\uD83E\uDD79\r\n\r\nApproach\r\nInitialization of Variables:\r\n\r\nInitialize four variables to keep track of the largest (
**PLSSS UPVOTE**\uD83E\uDD79\uD83E\uDD79\uD83E\uDD79\r\n\r\n**Approach**\r\nInitialization of Variables:\r\n\r\nInitialize four variables to keep track of the largest (biggest) and second-largest (secondBiggest) numbers, as well as the smallest (smallest) and second-smallest (secondSmallest) numbers. Initially, set biggest and secondBiggest to zero, and smallest and secondSmallest to the maximum representable integer value (INT_MAX).\r\nIteration Through the Array:\r\n\r\nIterate through each element of the input array nums.\r\nFor each element, compare it with the current largest (biggest) and smallest (smallest) values, updating the variables accordingly.\r\nAlso, update the second-largest (secondBiggest) and second-smallest (secondSmallest) values as necessary.\r\nCalculation of Products:\r\n\r\nAfter iterating through the entire array, the variables biggest, secondBiggest, smallest, and secondSmallest hold the largest, second-largest, smallest, and second-smallest numbers encountered in the array.\r\nCalculate the product difference by subtracting the product of the smallest and second-smallest numbers from the product of the largest and second-largest numbers: (biggest * secondBiggest) - (smallest * secondSmallest).\r\nReturn the Result:\r\n\r\nThe final result of the maxProductDifference algorithm is the calculated product difference, representing the maximum difference between the product of the largest and second-largest numbers and the product of the smallest and second-smallest numbers in the given array nums. Return this result as the output of the algorithm.\r\n\r\n# Code\r\n```\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n        int max1=0,max2=0,min1=100001,min2=100001;\r\n        for(auto &it:nums){\r\n            if(it>max1){\r\n                max2=max1;\r\n                max1=it;\r\n            }\r\n            else if(max2<it){\r\n                max2=it;\r\n            }\r\n            if(it<min1){\r\n                min2=min1;\r\n                min1=it;\r\n            }\r\n            else if(min2>it){\r\n                min2=it;\r\n            }\r\n        }\r\n        return (max1*max2)-(min1*min2);\r\n\r\n    }\r\n};\r\n```
['C++']