Problem 814
Maximum Product of Three Numbers
Easy · 10 pts · Array, Math, Sorting
Problem
Choose three elements at different positions of the array and maximise their product. Negative numbers matter: two large negative numbers multiply to a large positive number. After sorting, the answer is the larger of the product of the three largest elements and the product of the two smallest elements with the largest one. A single linear pass tracking the top three and bottom two values also works.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the maximum product of three elements.
Constraints
3 <= N <= 100000; -1000 <= A[i] <= 1000
Example input
4 -10 -10 5 2
Example output
500
Explanation
For -10 -10 5 2, the product of the two -10s and 5 is 500, larger than any product of three of the others. So the answer is 500.