✅Beats 100% Easy Two Method - C++ solution || With Time complexity and Space complexity ||
\r\n# PLEASE UPVOTE IF IT HELPED\r\n\r\n---\r\n\r\n\r\n# First Approach\r\n Describe your approach to solving the problem. \r\n# Complexity\r\n- Time complexity
\r\n# **PLEASE UPVOTE IF IT HELPED**\r\n\r\n---\r\n\r\n\r\n# First Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n# Complexity\r\n- Time complexity:O(nlogn)\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:O(1)\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\nclass Solution {\r\npublic:\r\n     int maxProductDifference(vector<int>& nums) {\r\n         int n=nums.size();\r\n         sort(nums.begin(),nums.end());\r\n         return ((nums[n-1]*nums[n-2])-(nums[0]*nums[1]));\r\n     }\r\n};\r\n\r\n```\r\n# Approach\r\n1. **Sort the Input Array (nums):**\r\n   - Arrange the elements of the input array `nums` in ascending order. This can be done using a sorting algorithm, such as quicksort or mergesort.\r\n\r\n2. **Identify the First Two Elements:**\r\n   - Select the first two elements of the sorted array. These two elements will be the minimum values in the array.\r\n\r\n3. **Calculate the Product of the First Two Elements:**\r\n   - Multiply the values of the first two elements to obtain the minimum product in the array.\r\n\r\n4. **Identify the Last Two Elements:**\r\n   - Select the last two elements of the sorted array. These two elements will be the maximum values in the array.\r\n\r\n5. **Calculate the Product of the Last Two Elements:**\r\n   - Multiply the values of the last two elements to obtain the maximum product in the array.\r\n\r\n6. **Compute the Difference Between Maximum and Minimum Products:**\r\n   - Subtract the minimum product (calculated in step 3) from the maximum product (calculated in step 5).\r\n\r\n7. **Return the Resulting Difference:**\r\n   - The calculated difference represents the result, which is the difference between the maximum product (of the last two elements) and the minimum product (of the first two elements) in the array.\r\n\r\n\r\n\r\n# Second Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:O(n)\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:O(1)\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n```\r\nclass Solution {\r\npublic:\r\n    int maxProductDifference(vector<int>& nums) {\r\n        int biggest = 0, secondBiggest = 0, smallest = INT_MAX, secondSmallest = INT_MAX;\r\n        \r\n        for (int num : nums) {\r\n            secondBiggest = (num > biggest) ? exchange(biggest, num) : max(secondBiggest, num);\r\n            secondSmallest = (num < smallest) ? exchange(smallest, num) : min(secondSmallest, num);\r\n        }\r\n        \r\n        return biggest * secondBiggest - smallest * secondSmallest;\r\n    }\r\n};\r\n```\r\n# Approach\r\n\r\n\r\n1. **Initialization of Variables:**\r\n   - Initialize 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\n\r\n2. **Iteration Through the Array:**\r\n   - Iterate through each element of the input array `nums`.\r\n   - For each element, compare it with the current largest (`biggest`) and smallest (`smallest`) values, updating the variables accordingly.\r\n   - Also, update the second-largest (`secondBiggest`) and second-smallest (`secondSmallest`) values as necessary.\r\n\r\n3. **Calculation of Products:**\r\n   - After 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\n   - Calculate 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\n\r\n4. **Return the Result:**\r\n   - The 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\r\n\r\n![imgleetcode.png](https://assets.leetcode.com/users/images/d4643fa4-865f-4445-aa90-4d272edee0aa_1702860631.1493733.png)\r\n
['Array', 'Sorting', 'C++']