#K11541. Maximum Product of Two Numbers
Maximum Product of Two Numbers
Maximum Product of Two Numbers
You are given an array of integers. Your task is to compute the maximum product of any two distinct elements in the array. If the array contains fewer than two elements, the answer is 0
.
Formally, if the input array is \(A = [a_1, a_2, \dots, a_n]\), you are to find \[ \max_{1 \le i < j \le n} (a_i \times a_j), \] with the convention that if \(n < 2\) then the answer is 0.
Examples:
- For
nums = [3, 4, 5, 2]
, the maximum product is20
. - For
nums = [1, 5, 3, 2, 4]
, the maximum product is20
. - For
nums = [1, 1, 1, 1]
, the maximum product is1
. - For
nums = [9]
or an empty array, the answer is0
.
inputFormat
The input is given via standard input. The first line contains an integer n which is the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single integer on standard output, which is the maximum product of any two distinct elements in the array. If the array contains fewer than two elements, output 0
.
4
3 4 5 2
20
</p>