#C8566. Maximum Product of Three

    ID: 52562 Type: Default 1000ms 256MiB

Maximum Product of Three

Maximum Product of Three

You are given an array of integers. Your task is to compute the maximum product of any three distinct numbers from the array. If the array contains fewer than three elements, output "Not enough elements".

The maximum product is defined as:

\( ans = \max(a_{n-1} \times a_{n-2} \times a_{n-3},\; a_0 \times a_1 \times a_{n-1}) \)

where the array is sorted in non-decreasing order \(a_0 \le a_1 \le \dots \le a_{n-1}\). This formula considers both the product of the three largest numbers and the product of the two smallest (possibly negative) numbers with the largest number.

Each test case is processed independently.

inputFormat

The first line contains an integer \(T\) representing the number of test cases.

For each test case, the first line contains an integer \(N\) which is the number of elements in the array. The next line contains \(N\) space-separated integers representing the array.

Example:

2
6
1 2 3 4 5 6
4
-10 -10 5 2

outputFormat

For each test case, output a single line that contains the maximum product of any three integers from the array. If the array has fewer than three elements, output "Not enough elements".

Example:

120
500
## sample
2
6
1 2 3 4 5 6
4
-10 -10 5 2
120

500

</p>