#C7178. Maximum Product of Three Numbers
Maximum Product of Three Numbers
Maximum Product of Three Numbers
You are given a list of integers. Your task is to determine the maximum product that can be obtained by multiplying any three distinct elements from the list. If the list contains fewer than 3 numbers, output -1
.
Mathematically, if the sorted list is \(a_0, a_1, \ldots, a_{n-1}\), the result is given by:
\[ \text{result} = \max\{a_{n-1} \times a_{n-2} \times a_{n-3},\;a_0 \times a_1 \times a_{n-1}\} \]
Ensure your solution reads input from standard input (stdin) and prints the result on standard output (stdout).
inputFormat
The first line contains an integer n representing the number of elements in the list. The second line contains n space-separated integers.
outputFormat
Output a single integer representing the maximum product of any three numbers. If the list contains fewer than 3 numbers, output -1
.
4
1 2 3 4
24