🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\nC++ []\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int n = nu
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/0d6bf656-8dfc-42b6-83f6-9cc3796ce182_1716951518.6194615.jpeg)\n![Screenshot 2024-05-29 082750.png](https://assets.leetcode.com/users/images/bf18f4aa-9835-4861-b2e3-80b5c3721fd8_1716951527.686214.png)\n![Screenshot 2024-05-29 082756.png](https://assets.leetcode.com/users/images/452c7a52-05bf-482a-8480-4fb76c188a24_1716951534.3214576.png)\n![Screenshot 2024-05-29 082803.png](https://assets.leetcode.com/users/images/0413ece5-2f88-4f16-905a-085324148a79_1716951540.2514117.png)\n![Screenshot 2024-05-29 082809.png](https://assets.leetcode.com/users/images/f3fb875c-a8a6-4556-b992-3437c11fa2d8_1716951546.3100522.png)\n```C++ []\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        return max(nums[n - 1] * nums[n - 2] * nums[n - 3],\n                   nums[0] * nums[1] * nums[n - 1]);\n    }\n};\n```\n```python3 []\nclass Solution:\n    def maximumProduct(self, nums: List[int]) -> int:\n        first_max = float(\'-inf\')\n        second_max = first_max\n        third_max = first_max\n\n        first_min = float(\'inf\')\n        second_min = first_min\n\n        for i in range(len(nums)):\n            if first_max <= nums[i]:\n                third_max = second_max\n                second_max = first_max\n                first_max = nums[i]\n            elif second_max <= nums[i]:\n                third_max = second_max\n                second_max = nums[i]\n            elif third_max <= nums[i]:\n                third_max = nums[i]\n\n            if(first_min >= nums[i]):\n                second_min = first_min\n                first_min = nums[i]\n            elif second_min >= nums[i]:\n                second_min = nums[i]\n        \n        return max(first_max * second_max * third_max, first_min * second_min * first_max)\n```\n```C []\nint maximumProduct(int* nums, int numsSize) {\n    int first_max = INT_MIN;\n    int second_max = first_max;\n    int third_max = first_max;\n\n    int first_min = INT_MAX;\n    int second_min = first_min;\n\n    for (int i = 0; i < numsSize; i++) {\n        // Calculating first_max, second_max, and third_max\n        if (first_max <= nums[i]) {\n            third_max = second_max;\n            second_max = first_max;\n            first_max = nums[i];\n        } else if (second_max <= nums[i]) {\n            third_max = second_max;\n            second_max = nums[i];\n        } else if (third_max <= nums[i])\n            third_max = nums[i];\n\n\n        // Calculating the first_min, second_min\n        if (first_min >= nums[i]) {\n            second_min = first_min;\n            first_min = nums[i];\n        } else if (second_min >= nums[i])\n            second_min = nums[i];\n    \n    }\n    int maxi = fmax(first_max * second_max * third_max, first_min * second_min * first_max);\n\n    return maxi;\n}\n```\n```Java []\nclass Solution {\n    public int maximumProduct(int[] nums) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        return Math.max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]);\n    }\n}\n```\n```python []\nclass Solution(object):\n    def maximumProduct(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        min1 = float(\'inf\')\n        min2 = float(\'inf\')\n        max1 = float(\'-inf\')\n        max2 = float(\'-inf\')\n        max3 = float(\'-inf\')\n        if len(nums) < 3:\n            return 0\n        \n        for num in nums:\n            if num < 0:\n                if num < min2:\n                    if num <= min1:\n                        min2 = min1\n                        min1 = num\n                    else:\n                        min2 = num\n            if num > max3:\n                if num > max2:\n                    if num >= max1:\n                        max3 = max2\n                        max2 = max1\n                        max1 = num\n                    else:\n                        max3 = max2\n                        max2 = num\n                else:\n                    max3 = num\n\n        if min1 <= 0 and min2 <= 0 and max1 >= 0 and min1 * min2 > max2*max3:\n            return min1 * min2 * max1\n        return max1 * max2 * max3\n```\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n```\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        return max(nums[n - 1] * nums[n - 2] * nums[n - 3],\n                   nums[0] * nums[1] * nums[n - 1]);\n    }\n};\n```\n# ***Please upvote if it\'s useful for you***\n![th.jpeg](https://assets.leetcode.com/users/images/0c448f43-8f64-4dac-922e-25881ac204d6_1716951675.5138948.jpeg)\n
['Array', 'Math', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3']