Maximum Product of Three Numbers
Maximum Product of Three Numbers asks you to find the largest possible product you can get by multiplying any three numbers from an integer array. The array can contain positive numbers, negative numbers, and zeros. You must choose exactly three elements, and the result should be the maximum product achievable.
This sounds straightforward until you remember how negative numbers behave. A negative times a negative becomes positive, which means two large-magnitude negative values can actually help you create a very large product when multiplied by a positive number.
That’s the twist interviewers care about. They want to see whether you can reason about sign changes and edge cases instead of assuming the answer is always the product of the three largest numbers.
Why “pick the three largest values” can be wrong
If all numbers are positive, the answer is simply the product of the three largest values.
But as soon as negatives are involved, the best product might come from using two negatives and one positive.
For example, if the array contains very small (high-magnitude) negatives like -10 and -9, multiplying them gives 90, which can beat a product made from smaller positive values.
So there are two main candidate shapes for the maximum product:
- One candidate uses the three largest numbers, which is what you’d expect if positives dominate.
- The other candidate uses the two smallest numbers (the most negative values) and the largest number, because the two negatives can turn into a large positive contribution.
Once you understand these two patterns, the problem becomes much simpler.
Want to explore more coding problem solutions? Check out the All Paths From Source to Target and Top K Frequent Words.
The key idea behind the solution
The maximum product of three numbers in an array will always be one of these two options:
- The product of the three largest numbers.
- Or the product of the largest number and the two smallest numbers.
You do not need to consider any other mix, because any maximum product must involve either the largest positives or the most powerful “double negative” effect.
This is the core reasoning that makes the solution clean and reliable.
How to compute the answer efficiently
A common interview-friendly approach is to sort the array.
After sorting:
- The three largest numbers are at the end.
- The two smallest numbers are at the beginning.
So you compute the product of the last three elements and compare it to the product of the first two elements with the last element. The larger of these two values is the answer.
Sorting makes the logic very easy to explain, which is often a plus in interviews.
There’s also a faster approach that avoids sorting by tracking the top three maximum values and the bottom two minimum values in a single pass. That achieves linear time and is a good option if the interviewer asks for optimization. Conceptually, it’s the same idea: you still compare those same two candidate products, you just gather the needed values without sorting.
Why these two candidates cover every case
To maximize a product of three numbers, you want large magnitudes.
- If you use three positive numbers, you want the three largest positives.
- If you involve negative numbers, using exactly one negative would make the product negative, which can’t be the maximum if there is any non-negative product available.
So the only negative-inclusive scenario that can produce a strong maximum is using two negatives, because their product is positive. And to maximize that effect, you choose the two most negative values, which are the smallest numbers in the array.
That’s why comparing these two candidates is enough to guarantee correctness.
Common edge cases to keep in mind
- If the array contains zeros, the maximum product might be zero when all other possible products are negative.
- If all numbers are negative, the maximum product is still found by selecting the three numbers that produce the least negative result, which ends up being the product of the three largest numbers in the sorted order.
- If the array has exactly three elements, the product of those three is the only answer.
These cases are automatically handled by the “two candidate products” idea, as long as you compute them correctly.
Performance in simple terms
- If you sort, the time cost is dominated by sorting.
- If you do a single pass with tracked minima and maxima, the time is linear.
- Space usage can be constant in both approaches, aside from the sort operation, depending on language implementation.
Top comments (0)