Finding the GOLDEN Trio ✨ | 2 Methods 🔥 | Simple Explanation + Illustration ⭐ | JAVA 🚀
IntuitionTo find the maximum product of any three numbers in an array, we must consider two scenarios:The three largest numbers in the array.
The two smallest n
# Intuition
To find the maximum product of any three numbers in an array, we must consider two scenarios:

The three largest numbers in the array.
The two smallest numbers (most negative) multiplied by the largest number. This is because two negative numbers produce a positive product, which might be larger than the product of the three largest numbers.

# Approach
Track Extremes:

Use variables to keep track of the three largest numbers (max1, max2, max3) and the two smallest numbers (min1, min2).
Single Pass:

Traverse the array once:
Update max1, max2, max3 whenever a larger number is found.
Update min1, min2 whenever a smaller number is found.
Compute the Maximum Product:

The result is the maximum of:
The product of the three largest numbers: max1 * max2 * max3.
The product of the two smallest numbers and the largest number: min1 * min2 * max1.
Return the Result:

Use Math.max to determine the larger of the two products.

# Illustration:
Consider the array: [-10, -10, 5, 2].

Initialize:

max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE
min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE
Traverse the array:

-10: Update min1 = -10, min2 = Integer.MAX_VALUE
-10: Update min2 = -10, min1 = -10
5: Update max1 = 5, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE
2: Update max2 = 2, max3 = Integer.MIN_VALUE
Compute products:

max1 * max2 * max3 = 5 * 2 * -10 = -100
max1 * min1 * min2 = 5 * -10 * -10 = 500
Result:

Maximum product = 500.

# Complexity
- Time complexity:
O(n)

- Space complexity:
<O(1)

# Code

# METHOD 1 (Single pass through array)
```java []
class Solution {
    public int maximumProduct(int[] nums) {
        int max1 = Integer.MIN_VALUE;
        int max2 = Integer.MIN_VALUE;
        int max3 = Integer.MIN_VALUE;
        int min1 = Integer.MAX_VALUE;
        int min2 = Integer.MAX_VALUE;
        for(int i=0; i<nums.length; i++)
        {
            if(nums[i] >= max1){
                max3 = max2;
                max2 = max1; 
                max1 = nums[i];
            }

            else if(nums[i] >= max2){
                max3 = max2;
                max2 = nums[i];
            }

            else if(nums[i] > max3) max3 = nums[i];


            if(nums[i] <= min1){
                min2 = min1;
                min1 = nums[i];
            }

            else if(nums[i] < min2) min2 = nums[i];

        }

        return Math.max(max1 * max2 * max3,max1 * min1 * min2);
    }
}
```


# METHOD 2 (Basic SORTING)

This is basically just sorting and checking if the product of the top 3 numbers are more or the product of the first number and the last two numbers are maximum, **However this takes more time.**

**Time complexity -> O(nlogn)**
**Space complexity -> O(1)**

```java []
import java.util.Arrays;

class Solution {
    public int maximumProduct(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        return Math.max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]);
    }
}
```


Hope this solution clears everything up! If you found it helpful, give it a thumbs up and don’t hesitate to reach out if you have any more questions or need further guidance. Keep pushing forward—your progress is just beginning! 🚀

![meow.png](https://assets.leetcode.com/users/images/dc78004f-4af1-4869-ab9b-02e876e9e516_1737901590.1806374.jpeg)



['Array', 'Math', 'Java']