c++ solution | with proper explaination | O(n) solution
The basic approach  that we all will first click is that just find the largest three numbers from the given vector nums and calculate the product-> and this wor
The **basic approach**  that we all will first click is that just find the largest three numbers from the given vector nums and calculate the product-> and this works to some extent only if the given vector nums is having positive elements\nbut what if there are negative elements too this basic approach will fail hence we need to think a better approach  from this.\n\n**Better Approach** is to think about the negative elements too. So we know if there are two  most negative elements -> it will give us a positive number. \n\nTake 5 variables \nmin1 , min2 -> initialized to INT_MAX\nmax1, max2, max3 ->initialized to INT_MIN\nfind the maximum of (min1* min2* max1 ), max1* max2* max3\nthe max we will get from above is our result.\n**Time complexity of the solution is O(N)**\n**Space Complexity - O(N)**\n\n```\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        // find out the min1 , min2, max1, max2, max3\n        int min1 = INT_MAX;\n        int min2= min1;\n        \n        int max1 = INT_MIN;\n        int max2 = max1; \n        int max3 = max1;\n        for(int i =0; i< nums.size(); i++){\n         \n            int val = nums[i];\n            // check for maximum \n            if(val >= max1){\n                max3 = max2; \n                max2 = max1;\n                max1 = val;\n            }\n            else if( val >= max2){ // there is a possibilty where val can be smaller than max1 and greater than or equal to max2 \n                max3 = max2;\n                max2 = val;\n            }\n            else if(val >= max3){ // there is a possibilty that the val can be smaller than max1 & max2 but greater than or equal to max3\n                max3 = val;\n            }\n            \n            // check for minimum \n            if(val <= min1){\n              min2 = min1;\n              min1 = val;\n            }else if(val <= min2) {\n               min2 = val;\n\n            }\n        }\n        int max_product = max((min1* min2* max1),(max1* max2* max3));\n        return max_product;\n    }\n};\n```\n**Do upvote if you like the solution and comment if have doubts**
['C', 'C++']