BEATS 100%, O(N), SOLUTION
Intuition 🤔To find the maximum product of three numbers in an array, we need to consider both the largest three numbers (positive or negative) and the smallest
# Intuition 🤔
To find the maximum product of three numbers in an array, we need to consider both the largest three numbers (positive or negative) and the smallest two numbers (in case they are negative) combined with the largest number. This ensures that we capture both possible scenarios for the highest product.

# Approach 🚀
1. **Tracking Maximums and Minimums**:
   - Use three variables (`m1`, `m2`, `m3`) to keep track of the **three largest numbers** in the array.
   - Use two variables (`n1`, `n2`) to keep track of the **two smallest numbers** in the array.

2. **Single Pass**:
   - Iterate through the array once.
   - For each number, update the three largest numbers (`m1`, `m2`, `m3`) and two smallest numbers (`n1`, `n2`) efficiently.

3. **Calculate Maximum Product**:
   - Compute two potential products:
     - Product of the top three largest numbers: `m1 * m2 * m3`.
     - Product of the two smallest numbers (which could be negative) and the largest number: `n1 * n2 * m1`.

4. **Return the Maximum**:
   - Compare the two products and return the larger one.

# Complexity 📊
- **Time Complexity**:  
  $$O(n)$$ - We iterate through the array once to find the largest and smallest numbers.

- **Space Complexity**:  
  $$O(1)$$ - Only a constant amount of extra space is used for variables.


# Code
```cpp []
//OPTIMAL T=O(n) S=O(1)
class Solution {
public:
    int maximumProduct(vector<int>& nums) {
        // Initialize the largest and smallest values
        int m1 = INT_MIN, m2 = INT_MIN, m3 = INT_MIN; 
        int n1 = INT_MAX, n2 = INT_MAX;         

        for (int x : nums) {
            // Update largest three numbers
            if (x > m1) {
                m3 = m2;
                m2 = m1;
                m1 = x;
            } else if (x > m2) {
                m3 = m2;
                m2 = x;
            } else if (x > m3) {
                m3 = x;
            }

            // Update smallest two numbers
            if (x < n1) {
                n2 = n1;
                n1 = x;
            } else if (x < n2) {
                n2 = x;
            }
        }

        // Calculate both potential maximum products
        int product1 = m1 * m2 * m3; 
        int product2 = m1 * n1 * n2; 

        return max(product1, product2);
    }
};
//BRUTE FORCE T=O(n*logn) S=O(1)
// class Solution {
// public:
//     int maximumProduct(vector<int>& nums) {
//         sort(nums.begin(), nums.end());
//         int n = nums.size();
//         int p1=nums[n-1], p2=1, j=n-1, i=0;
//         while(i<3){
//             p2 *= nums[j];
//             if(i<2)
//                 p1 *= nums[i];
//             j--;
//             i++;
//         }
//         return max(p1, p2);
//     }
// };

```
['C++']