Beats 100% of users with C++ || Step By Step Explain || Easy to Understand || Best Approach ||
Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it...\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this problem.
# Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it...\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem asks for the maximum product that can be obtained by multiplying three integers from the given array. To achieve this, we can observe that the maximum product would be the product of the three largest numbers in the array or the product of the two smallest numbers (if they are negative) and the largest number (to handle negative numbers).\n\n---\n\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/227700c2-8c45-4442-9319-e08bbd3572b5_1708108729.3926651.png)\n\n\n---\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize variables to keep track of the three maximum and two minimum numbers encountered.\n- Iterate through the array:\n    - Update the maximum and minimum values accordingly.\n- After iterating through the array, return the maximum of the product of the three maximum numbers and the product of the two minimum numbers and the maximum number.\n\n\n---\n\n# Complexity\n\n- **Time complexity:** O(n), where n is the size of the input array. We iterate through the array once.\n- **Space complexity:**  O(1), as we use only a constant amount of extra space regardless of the size of the input array.\n\n---\n# Code\n```\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) \n    {\n        int first_max = INT_MIN ;\n        int second_max = INT_MIN ;\n        int third_max = INT_MIN ;\n\n        int first_min = INT_MAX ;\n        int second_min = INT_MAX ;\n\n        for( int i = 0 ; i < nums.size() ; i++ )\n        {\n            if( nums[i] < first_min )\n            {\n                second_min = first_min ;\n                first_min = nums[i] ;\n            }\n            else if( nums[i] < second_min )\n                second_min = nums[i] ;\n            if( nums[i] > first_max )\n            {\n                third_max = second_max ;\n                second_max = first_max ;\n                first_max = nums[i] ;\n            }\n            else if( nums[i] > second_max )\n            {\n                third_max = second_max ;\n                second_max = nums[i] ;\n            }\n            else if( nums[i] > third_max )\n                third_max = nums[i] ;\n        }\n        return max ( first_max * second_max * third_max , first_min * second_min * first_max );\n    }\n};\n```\n\n---\n---\n# if you like the solution please UPVOTE it...\n\n\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/fae1eac9-e29d-4802-a977-571106b784c8_1708108841.1077046.gif)\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/9f479cfa-0368-4d85-a649-dade6521bc16_1708108843.0961788.gif)\n\n---\n---\n---
['Array', 'Math', 'Sorting', 'C++']