✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(with Sort)\n1. sorts the input array nums in ascending orde
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n*(Also explained in the code)*\n\n#### ***Approach 1(with Sort)***\n1. sorts the input array `nums` in ascending order.\n1. Computes the difference between the product of the last two elements and the product of the first two elements in the sorted array.\n 1. Returns the calculated difference between the maximum product (of the last two elements) and the minimum product (of the first two elements) in the array.\n\n# Complexity\n- Time complexity:\n   $$O(nlogn)$$\n    \n\n- Space complexity:\n   $$O(logn)$$ or $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int n =nums.size();\n\n        return nums[n-2]*nums[n-1]-nums[0]*nums[1];\n    }\n};\n\n\n```\n\n```Java []\nclass Solution {\n    public int maxProductDifference(int[] nums) {\n        Arrays.sort(nums);\n        return nums[nums.length - 1] * nums[nums.length - 2] - nums[0] * nums[1];\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        nums.sort()\n        return nums[-1] * nums[-2] - nums[0] * nums[1]\n\n\n```\n```javascript []\nfunction maxProductDifference(nums) {\n    nums.sort((a, b) => a - b);\n    const n = nums.length;\n\n    return nums[n - 2] * nums[n - 1] - nums[0] * nums[1];\n}\n\n\n\n```\n---\n#### ***Approach 2(Keeping track of 2 largest and 2 smallest)***\n1. `maxProductDifference` finds 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`.\n1. It iterates through the array, keeping track of the largest, second-largest, smallest, and second-smallest numbers encountered.\n1. Finally, it computes the product difference of these values and returns the result.\n\n# Complexity\n- Time complexity:\n   $$O(n)$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        // Initialize variables to hold the largest, second-largest, smallest, and second-smallest numbers\n        int biggest = 0;\n        int secondBiggest = 0;\n        int smallest = INT_MAX;\n        int secondSmallest = INT_MAX;\n        \n        // Iterate through the input array \'nums\'\n        for (int num : nums) {\n            // Check if \'num\' is greater than the current \'biggest\'\n            if (num > biggest) {\n                // Update \'secondBiggest\' and \'biggest\' accordingly\n                secondBiggest = biggest;\n                biggest = num;\n            } else {\n                // If \'num\' is not the largest, update \'secondBiggest\' if necessary\n                secondBiggest = max(secondBiggest, num);\n            }\n            \n            // Check if \'num\' is smaller than the current \'smallest\'\n            if (num < smallest) {\n                // Update \'secondSmallest\' and \'smallest\' accordingly\n                secondSmallest = smallest;\n                smallest = num;\n            } else {\n                // If \'num\' is not the smallest, update \'secondSmallest\' if necessary\n                secondSmallest = min(secondSmallest, num);\n            }\n        }\n        \n        // Calculate the product difference of the largest and smallest values and return the result\n        return biggest * secondBiggest - smallest * secondSmallest;\n    }\n};\n\n\n\n```\n```Java []\nclass Solution {\n    public int maxProductDifference(int[] nums) {\n        int biggest = 0;\n        int secondBiggest = 0;\n        int smallest = Integer.MAX_VALUE;\n        int secondSmallest = Integer.MAX_VALUE;\n        \n        for (int num : nums) {\n            if (num > biggest) {\n                secondBiggest = biggest;\n                biggest = num;\n            } else {\n                secondBiggest = Math.max(secondBiggest, num);\n            }\n            \n            if (num < smallest) {\n                secondSmallest = smallest;\n                smallest = num;\n            } else {\n                secondSmallest = Math.min(secondSmallest, num);\n            }\n        }\n        \n        return biggest * secondBiggest - smallest * secondSmallest;\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        biggest = 0\n        second_biggest = 0\n        smallest = inf\n        second_smallest = inf\n        \n        for num in nums:\n            if num > biggest:\n                second_biggest = biggest\n                biggest = num\n            else:\n                second_biggest = max(second_biggest, num)\n                \n            if num < smallest:\n                second_smallest = smallest\n                smallest = num\n            else:\n                second_smallest = min(second_smallest, num)\n        \n        return biggest * second_biggest - smallest * second_smallest\n\n\n```\n```javascript []\n\n\n\n```\n---\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Array', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript']