Maximum Product of Three Numbers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The maximum product of three numbers problem looks simple until negative numbers enter the picture. The best answer is not always the product of the three largest values, because two very small negative numbers can multiply into a large positive number and beat that obvious choice.
The Key Observation
For any integer array, the maximum product of three numbers must be one of these two candidates:
- the product of the three largest numbers
- the product of the two smallest numbers and the largest number
The second case matters because if the two smallest values are large-magnitude negatives, their product becomes positive.
For example:
- array:
[-10, -10, 1, 2, 5] - three largest:
1 * 2 * 5 = 10 - two smallest and largest:
-10 * -10 * 5 = 500
The second product wins.
Sorting-Based Solution
The most direct solution is to sort the array and compare those two candidate products:
This is clean and easy to reason about. The cost is O(n log n) because of the sort.
One-Pass O(n) Solution
If you want linear time, track:
- the three largest values seen so far
- the two smallest values seen so far
Then compare the same two candidate products at the end:
This avoids sorting and runs in O(n) time with O(1) extra space.
Why the Logic Is Enough
You do not need to inspect every triple. The maximum product cannot come from an arbitrary middle combination once you realize the sign behavior:
- large positives help
- large-magnitude negatives help only in pairs
- the largest positive value is always a strong candidate in the winning triple
That is why the entire problem collapses to the two-candidate comparison above.
Edge Cases
If the array has exactly three elements, the answer is simply their product.
If all numbers are negative, the result can still be negative. In that situation, the best product is the one closest to zero among valid triples, which the same algorithm still handles correctly.
If zeros are present, they matter naturally. A zero product can beat a negative product when no positive triple exists.
Common Pitfalls
The most common mistake is returning the product of the three largest values without considering the pair of smallest negatives.
Another issue is assuming the answer must be positive. Arrays with only negative values or too many zeros can produce non-positive maximum products.
Developers also overcomplicate the solution with brute force when the two-candidate rule already captures the entire search space.
Summary
- The answer is always the maximum of two candidate products.
- Compare the product of the three largest values with the product of the two smallest values and the largest value.
- Sorting gives a simple
O(n log n)solution. - Tracking top three and bottom two values gives an
O(n)solution. - Negative numbers are the reason the problem is more interesting than it first appears.

