#K39327. Maximum Product of Three Numbers

    ID: 26396 Type: Default 1000ms 256MiB

Maximum Product of Three Numbers

Maximum Product of Three Numbers

Given a list of integers, the task is to compute the maximum product obtainable by multiplying any three numbers from the list. If the list contains fewer than three numbers, the answer is -1. Note that the product can be influenced by negative numbers. In particular, if the two smallest (possibly negative) numbers and the largest number yield a product greater than the product of the three largest numbers, then that is the answer.

The mathematical formulation of the problem is as follows:

Let \(a_1, a_2, ..., a_n\) be the input numbers. If \(n < 3\), output \(-1\). Otherwise, compute:

\[ \text{result} = \max\left(a_{(1)} \times a_{(2)} \times a_{(n)},\; a_{(n-2)} \times a_{(n-1)} \times a_{(n)}\right) \]

where \(a_{(1)} \leq a_{(2)} \leq ... \leq a_{(n)}\) is the sorted order of the input.

inputFormat

The first line contains an integer \(n\) representing the number of integers. The second line contains \(n\) space-separated integers.

outputFormat

Output a single integer which is the maximum product obtainable by multiplying any three of the given numbers. If \(n < 3\), output \(-1\).

## sample
4
4 1 3 2
24