Maximum Product Difference Between Two Pairs
Intuition\nFinding the first largest and second largest element in the array and product it . then find first & second smallest element in an array and then pro
# Intuition\nFinding the first largest and second largest element in the array and product it . then find first & second smallest element in an array and then product it at last return the largestelement-smallestelement\n\n# Approach\nsort(nums.begin(), nums.end());: The input vector nums is sorted in ascending order using the sort function from the C++ Standard Template Library (STL).\n\nint lar = nums[n-1] * nums[n-2];: The product of the two largest elements in the sorted vector is calculated and stored in the variable lar.\n\nint sec = nums[0] * nums[1];: The product of the two smallest elements in the sorted vector is calculated and stored in the variable sec.\n\nreturn lar - sec;: The difference between the maximum and minimum products is calculated and returned.\n\n# Complexity\n- Time complexity:\nTime Complexity:\n\nThe most significant factor in terms of time complexity is the sorting operation, which has a time complexity of O(n log n), where "n" is the size of the input vector.\nOther operations, such as accessing elements in the vector and basic arithmetic operations, have a time complexity of O(n).\nTherefore, the overall time complexity of the maxProductDifference function is O(n log n).\n\n- Space complexity:\n\nThe space complexity of the code is relatively low. It uses a constant amount of extra space for the integer variables n, lar, and sec.\nThe sorting operation typically has a space complexity of O(1) or O(log n) depending on the sorting algorithm used. In the case of the C++ sort function, it often uses an in-place sorting algorithm with O(1) additional space.\nTherefore, the overall space complexity of the maxProductDifference function is O(1).\nIn summary, the time complexity is O(n log n), and the space complexity is O(1).\n\n\n# Code\n```\n#include<bits/stdc++.h>\nclass Solution {\npublic:\n    int maxProductDifference(vector<int>& nums) {\n        int n = nums.size();\n        sort(nums.begin(),nums.end());\n        int lar = nums[n-1]*nums[n-2];\n        int sec  = nums[0]*nums[1];\n        return lar-sec;\n        \n    }\n};\n```
['C++']