Best Solution in C++ for Trees 🚀 || 100% working
IntuitionTo find the maximum product, we should consider the largest numbers. But what if the array has negative numbers? 🤔 Negative numbers can flip the result
# Intuition  
To find the maximum product, we should consider the largest numbers. But what if the array has negative numbers? 🤔 Negative numbers can flip the result! So, we also check the smallest two negatives multiplied with the largest positive number. 🚀

# Approach  
1. Sort the array. 🧹  
2. Compare:  
   - The product of the three largest numbers.  
   - The product of the largest number and the two smallest numbers.  
3. Return the maximum of the two. 🌟

# Complexity  
- **Time complexity:** $$O(n \log n)$$ (because of sorting).  
- **Space complexity:** $$O(1)$$ (in-place sorting).  

# Code  
```cpp
class Solution {
public:
    int maximumProduct(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        return max(nums[n-1] * nums[n-2] * nums[n-3], nums[n-1] * nums[0] * nums[1]);
    }
};
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Array', 'Math', 'Sorting', 'C++']