Without Sorting || Easy to UnderStand || 99% Beats
Intuition\nBy maintaining the three largest and two smallest numbers while iterating through the array, we can compute the maximum product without sorting the a
# Intuition\nBy maintaining the three largest and two smallest numbers while iterating through the array, we can compute the maximum product without sorting the array.\n\n# Approach\n1. **Initialize Variables:** Use variables to store the three largest and two smallest numbers.\n2. **Single Pass Through Array:** Update these variables as we iterate through the array.\n3. **Calculate the Maximum Product:** Use the stored values to calculate the maximum product of three numbers.\n\n# Complexity\n- **Time Complexity:** $$O(n)$$  because we only traverse the array once.\n- **Space Complexity:** $$O(1)$$ because we use a constant amount of extra space.\n\n# Code\nHere\'s the implementation of the above approach:\n\n```cpp\nclass Solution {\npublic:\n    int maximumProduct(vector<int>& nums) {\n        int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;\n        int min1 = INT_MAX, min2 = INT_MAX;\n        \n        for (int num : nums) {\n            // Update the three largest values\n            if (num > max1) {\n                max3 = max2;\n                max2 = max1;\n                max1 = num;\n            } else if (num > max2) {\n                max3 = max2;\n                max2 = num;\n            } else if (num > max3) {\n                max3 = num;\n            }\n            \n            // Update the two smallest values\n            if (num < min1) {\n                min2 = min1;\n                min1 = num;\n            } else if (num < min2) {\n                min2 = num;\n            }\n        }\n        \n        // Maximum product is the maximum of the product of the three largest\n        // numbers and the product of the two smallest numbers with the largest number.\n        return max(max1 * max2 * max3, min1 * min2 * max1);\n    }\n};\n```\n\n# Beats\n![image.png](https://assets.leetcode.com/users/images/c253275f-7785-45e7-abca-4b35df553a2a_1719812756.4589894.png)\n\n\n# Explanation\n- **Initialization:**\n  - `max1`, `max2`, `max3` are initialized to `INT_MIN` to store the three largest numbers.\n  - `min1`, `min2` are initialized to `INT_MAX` to store the two smallest numbers.\n- **Single Pass Through Array:**\n  - Update `max1`, `max2`, `max3` if the current number is greater than any of these.\n  - Update `min1`, `min2` if the current number is smaller than any of these.\n- **Calculate Maximum Product:** Return the maximum of the product of the three largest numbers and the product of the two smallest numbers with the largest number.\n\nThis solution efficiently finds the maximum product of three numbers in $$O(n)$$ time complexity.
['Array', 'Math', 'C++']