Max Product Trio: Navigating Negatives for Maximum Gain Using C++
Intuition\nThe problem requires finding the maximum product of three numbers from the list. The key observation is that the maximum product could either come fr
# Intuition\nThe problem requires finding the maximum product of three numbers from the list. The key observation is that the maximum product could either come from the three largest numbers or from two smallest (negative) numbers and the largest number.\n\n# Approach\n1. Sort the array.\n2. Calculate the product of the two smallest numbers (which could be negative) and the largest number.\n3. Calculate the product of the three largest numbers.\n4. Return the maximum of these two products.\n\n# Complexity\n- Time complexity: $$O(n \\log n)$$, where $$n$$ is the size of the input array. The sorting step dominates the time complexity.\n- Space complexity: $$O(1)$$, as we are using constant extra space.\n\n# Code\n```cpp\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        int l = nums.size(); \n        sort(nums.begin(), nums.end()); \n        int withNeg = nums[0] * nums[1] * nums[l-1]; \n        int withoutNeg = nums[l-1] * nums[l-2] * nums[l-3]; \n        return max(withNeg, withoutNeg); \n    }\n};\n```\n
['C++']