#C13395. Product Except Self and Maximum Triple Product
Product Except Self and Maximum Triple Product
Product Except Self and Maximum Triple Product
You are given a list of integers. Your task is two-fold:
- For each index (i) (with (0 \le i < n)), compute (\text{result}[i] = \prod_{j\neq i} a_j), i.e. the product of all the other numbers except the (i)-th element. This can be represented in LaTeX as:
[ result[i] = \prod_{\substack{0 \le j < n \ j \neq i}} a_j ]
- Find the maximum product that can be obtained by multiplying any three distinct numbers from the list. Note that elements can be negative, so the maximum product might come from the product of two very small (negative) numbers and a large positive number.
Input will be provided via standard input and the output should be printed to standard output in the format described below. You may assume that the list contains at least two integers. In case of invalid input (which will not be part of the test cases), the behavior is undefined.
inputFormat
The input consists of two lines:
- The first line contains an integer (n) ((n \ge 2)), the number of elements in the list.
- The second line contains (n) space-separated integers.
outputFormat
The output consists of two lines:
- The first line contains (n) space-separated integers where the (i)-th integer is the product of all numbers in the list except the (i)-th one.
- The second line contains a single integer, which is the maximum product obtainable by multiplying any three distinct numbers from the list.## sample
4
1 2 3 4
24 12 8 6
24
</p>