#K53787. Maximum Product of Three Numbers
Maximum Product of Three Numbers
Maximum Product of Three Numbers
Given a list of integers, your task is to compute the maximum product of any three distinct integers. Formally, if the list is represented as \(a_1, a_2, \dots, a_n\), you need to find the maximum value of \(a_i \times a_j \times a_k\) where \(i, j, k\) are distinct indices.
Note: The input numbers can be negative, zero, or positive. Hence, the maximum product might come from either the product of the three largest numbers or from the product of the two smallest (possibly negative) numbers and the largest number.
Examples:
- For input
1 2 3 4
, the answer is24
because \(2 \times 3 \times 4 = 24\). - For input
-10 -10 1 3 2
, the answer is300
because \((-10) \times (-10) \times 3 = 300\). - For input
-1 -2 -3 -4
, the answer is-6
because \((-1) \times (-2) \times (-3) = -6\).
inputFormat
The input is given via standard input (stdin) as a single line containing space-separated integers. There is no additional information such as the number of integers.
outputFormat
Output the maximum product obtained by multiplying any three distinct numbers from the input. The answer should be written to standard output (stdout).
## sample1 2 3 4
24
</p>