#C6368. Maximum Product of Two Distinct Elements
Maximum Product of Two Distinct Elements
Maximum Product of Two Distinct Elements
You are given an integer n
and an array of n
integers. Your task is to compute the maximum product of any two distinct elements in the array under the condition that the array does not contain any duplicate elements. If there are any duplicate elements in the array, you should output -1
.
If the array contains only distinct numbers, the answer is computed as follows. Let the sorted array be \( a_1 \le a_2 \le \cdots \le a_n \). Then the maximum product is \( \max(a_1 \times a_2,\, a_{n-1} \times a_n) \). Note that the product of the two smallest numbers might be larger than the product of the two largest numbers when both of them are negative.
Input and Output Specifications:
Read the input from standard input and print the output to standard output.
inputFormat
The first line contains an integer n
(the number of elements in the array). The second line contains n
space-separated integers representing the elements of the array.
outputFormat
Print a single integer: the maximum product of any two distinct elements if the array contains only distinct numbers, otherwise print -1
.
5
5 1 2 3 4
20
</p>