#C13993. Maximum Product of Three Numbers

    ID: 43592 Type: Default 1000ms 256MiB

Maximum Product of Three Numbers

Maximum Product of Three Numbers

Given an array of integers, find three numbers such that their product is maximized. The array may contain both positive and negative numbers. Note that the maximum product might come from either:

  • the product of the three largest numbers, or
  • the product of the two smallest (possibly negative) numbers and the largest number.

The goal is to compute \(\max(a_{n-2} \times a_{n-1} \times a_{n},\ a_{1} \times a_{2} \times a_{n})\) after sorting the array.

If the array contains fewer than three numbers, output the error message: "Error: At least three numbers are required".

inputFormat

The input is read from standard input (stdin). The first line contains an integer (n) denoting the number of integers. The second line contains (n) space-separated integers.

outputFormat

Output a single line to standard output (stdout) containing the maximum product of any three numbers from the array. If the array does not contain at least three numbers, output "Error: At least three numbers are required".## sample

5
1 10 -5 1 -100
5000

</p>