#K78392. Highest Product of Three
Highest Product of Three
Highest Product of Three
Given an array of integers, your task is to find the highest product of any three numbers. If the array contains fewer than three numbers, print None
.
You can solve this problem by sorting the array and then comparing the product of the three largest numbers with the product of the largest number and the two smallest numbers. In mathematical terms, if the sorted array is (a_0 \leq a_1 \leq \cdots \leq a_{n-1}), the answer is given by:
[
\max(a_{n-1} \times a_{n-2} \times a_{n-3},; a_{n-1} \times a_0 \times a_1)]
This works because the product of two negative numbers becomes positive.
inputFormat
The input is read from stdin. The first line contains an integer (n) representing the number of integers. The second line contains (n) space-separated integers.
outputFormat
Print the highest product of any three numbers to stdout. If there are fewer than three numbers, print None
.## sample
6
1 10 2 6 5 3
300