#C5896. Maximum Product of Two Elements
Maximum Product of Two Elements
Maximum Product of Two Elements
You are given an array of integers. Your task is to find the maximum product that can be obtained by multiplying two distinct elements from the array.
This maximum product can be achieved by either multiplying the two largest numbers or, if negative values are present, multiplying the two smallest (most negative) numbers. Mathematically, if the sorted array is \(a_0 \leq a_1 \leq \dots \leq a_{n-1}\), then the answer is:
$$\max(a_{n-1} \times a_{n-2},\; a_0 \times a_1)$$
Note: The array will contain at least two elements.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains an integer \(n\) that represents the number of elements in the array.
- The second line contains \(n\) space-separated integers that form the array.
outputFormat
Output a single integer on stdout which is the maximum product obtainable by multiplying any two distinct elements of the array.
## sample4
1 2 3 4
12