2 Approach 🔥 || Easy C++ ✅ Solution || Brute Force [Sorting] and Optimized Approach ⚡
Hint\n\nHint 1: Scan the array and compute Maximum, second maximum, third maximum element, and minimum and second minimum element present in the array.\n\nHint 
# Hint\n\n**Hint 1:** Scan the array and compute Maximum, second maximum, third maximum element, and minimum and second minimum element present in the array.\n\n**Hint 2:** The answer will always be maximum of product of Maximum, second maximum and third maximum and product of Minimum, second minimum and Maximum element.\n\n\n\n\n# Approach 1: Brute Force Approach [Sorting]\n\n# Complexity\n- Time complexity: O(nlogn)\n\n- Space complexity: O(1)\n\n\n# Code\n```\n// Brute Force Approach\n// Time complexity -> O(nlogn) and Space -> O(1)\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        \n        int n=nums.size();\n        if(n<3)\n        {\n            return -1;\n        }\n        sort(nums.begin(),nums.end());\n\n        int maxi=max(nums[n-3]*nums[n-2]*nums[n-1],nums[0]*nums[1]*nums[n-1]);\n\n        return maxi;\n    }\n};\n```\n\n# Approach 2: Optimized Approach\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n**Steps**\n\n1. First calcualte all of this below value:\n\n    - first_max means Largest number in an array\n    - second_max means second largest number in an array\n    - third_max means third largest number in an array\n    - first_min means smallest number in an array\n    - second_min means second smallest number in an array\n\n2. Then the answer will always be maximum of product of first_max, second_max and third_max and product of first_min, second_min and first_max.\n\n\n# Code\n```\n// Optimized Approach\n// Time complexity -> O(n) and Space -> O(1)\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n\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<nums.size();i++)\n        {\n            // Calculating first_max, second_max, and third_max\n            if(first_max<=nums[i])\n            {\n                third_max=second_max;\n                second_max=first_max;\n                first_max=nums[i];\n            }\n            else if(second_max<=nums[i])\n            {\n                third_max=second_max;\n                second_max=nums[i];\n            }\n            else if(third_max<=nums[i])\n            {\n                third_max=nums[i];\n            }\n\n            // Calculating the first_min, second_min\n            if(first_min>=nums[i])\n            {\n                second_min=first_min;\n                first_min=nums[i];\n            }\n            else if(second_min>=nums[i])\n            {\n                second_min=nums[i];\n            }\n        }\n        int maxi=max(first_max*second_max*third_max,first_min*second_min*first_max);\n\n        return maxi;\n    }\n};\n```
['Sorting', 'C++']