✅Beats 100% - Explained with [ Video ] - Largest and Smallest - C++/Java/Python/JS  - Visualized
\r\n\r\n# YouTube Video Explanation:\r\n\r\nhttps://youtu.be/NMSo-GyBVJ4\r\n **If you want a video for this question please write in the comments** \r\n\r\n\uD8
![Screenshot 2023-12-18 081529.png](https://assets.leetcode.com/users/images/c159798a-2886-4666-b2a8-80355e0d38f5_1702867611.5961163.png)\r\n\r\n# YouTube Video Explanation:\r\n\r\n[https://youtu.be/NMSo-GyBVJ4](https://youtu.be/NMSo-GyBVJ4)\r\n<!-- **If you want a video for this question please write in the comments** -->\r\n\r\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making complex concepts easy to understand.**\r\n\r\nSubscribe Link: https://www.youtube.com/@leetlogics/?sub_confirmation=1\r\n\r\n*Subscribe Goal: 900 Subscribers*\r\n*Current Subscribers: 856*\r\n\r\n---\r\n\r\n# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nTo maximize the product difference between pairs, we need to select the largest and second-largest values for one pair and the smallest and second-smallest values for the other pair. By finding these extreme values, we ensure that the product difference is maximized.\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n1. Initialize variables to keep track of the largest (`largest`) and second-largest (`secondLargest`) values, as well as the smallest (`smallest`) and second-smallest (`secondSmallest`) values.\r\n2. Iterate through the array, updating the extreme values based on the current element.\r\n3. Calculate the product difference using the formula: `(largest * secondLargest) - (smallest * secondSmallest)`.\r\n4. Return the calculated product difference.\r\n\r\n# Complexity\r\n- Time complexity:  `O(N)` - We iterate through the array once.\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity: `O(1)` - We use a constant amount of space for variables.\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```java []\r\nclass Solution {\r\n    public int maxProductDifference(int[] nums) {\r\n        int firstBig = 0, secondBig = 0;\r\n        int firstSmall = Integer.MAX_VALUE, secondSmall = Integer.MAX_VALUE;\r\n\r\n        for (int n : nums) {\r\n            if (n < firstSmall) {\r\n                secondSmall = firstSmall;\r\n                firstSmall = n;\r\n            } else if (n < secondSmall) {\r\n                secondSmall = n;\r\n            }\r\n\r\n            if (n > firstBig) {\r\n                secondBig = firstBig;\r\n                firstBig = n;\r\n            } else if (n > secondBig) {\r\n                secondBig = n;\r\n            }\r\n        }\r\n\r\n        return firstBig * secondBig - firstSmall * secondSmall;        \r\n    }\r\n}\r\n```\r\n```C++ []\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n        int largest = 0, secondLargest = 0;\r\n        int smallest = INT_MAX, secondSmallest = INT_MAX;\r\n\r\n        for (int n : nums) {\r\n            if (n < smallest) {\r\n                secondSmallest = smallest;\r\n                smallest = n;\r\n            } else if (n < secondSmallest) {\r\n                secondSmallest = n;\r\n            }\r\n\r\n            if (n > largest) {\r\n                secondLargest = largest;\r\n                largest = n;\r\n            } else if (n > secondLargest) {\r\n                secondLargest = n;\r\n            }\r\n        }\r\n\r\n        return (largest * secondLargest) - (smallest * secondSmallest);\r\n    }\r\n};\r\n```\r\n```Python []\r\nclass Solution(object):\r\n    def maxProductDifference(self, nums):\r\n        largest, secondLargest = 0, 0\r\n        smallest, secondSmallest = float(\'inf\'), float(\'inf\')\r\n\r\n        for n in nums:\r\n            if n < smallest:\r\n                secondSmallest = smallest\r\n                smallest = n\r\n            elif n < secondSmallest:\r\n                secondSmallest = n\r\n\r\n            if n > largest:\r\n                secondLargest = largest\r\n                largest = n\r\n            elif n > secondLargest:\r\n                secondLargest = n\r\n\r\n        return (largest * secondLargest) - (smallest * secondSmallest)\r\n```\r\n```JavaScript []\r\n/**\r\n * @param {number[]} nums\r\n * @return {number}\r\n */\r\nvar maxProductDifference = function(nums) {\r\n    let largest = 0, secondLargest = 0;\r\n    let smallest = Number.MAX_SAFE_INTEGER, secondSmallest = Number.MAX_SAFE_INTEGER;\r\n\r\n    for (const n of nums) {\r\n        if (n < smallest) {\r\n            secondSmallest = smallest;\r\n            smallest = n;\r\n        } else if (n < secondSmallest) {\r\n            secondSmallest = n;\r\n        }\r\n\r\n        if (n > largest) {\r\n            secondLargest = largest;\r\n            largest = n;\r\n        } else if (n > secondLargest) {\r\n            secondLargest = n;\r\n        }\r\n    }\r\n\r\n    return (largest * secondLargest) - (smallest * secondSmallest);\r\n};\r\n```\r\n![upvote.png](https://assets.leetcode.com/users/images/b40f6a45-36d0-4413-b7dc-da9e1f198879_1702867628.425322.png)\r\n
['Array', 'Python', 'C++', 'Java', 'JavaScript']