#C8821. Maximum Product after Swaps
Maximum Product after Swaps
Maximum Product after Swaps
You are given an array \(A\) of length \(N\) containing integers. You are allowed to perform a swap operation between two elements \(A[i]\) and \(A[j]\) (with \(i < j\)) only if \(A[i] \neq A[j]\). After the swap, the product \(B_i \times B_j\), where \(B_i\) and \(B_j\) are the values at positions \(i\) and \(j\) respectively following the swap, is computed. Your task is to determine the maximum product obtainable from any valid swap operation.
Note that if all elements in the array are identical (making a valid swap impossible), the product of any two identical elements is still considered.
This problem essentially reduces to finding two elements in the array whose product is maximum; this could be achieved by either selecting the two largest elements or the two smallest (most negative) elements.
inputFormat
The input is given via standard input. The first line contains an integer \(T\), the number of test cases. For each test case, 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 \(A\).
outputFormat
For each test case, output a single integer on a new line denoting the maximum product obtained by selecting two elements (after performing a swap if needed) that satisfy the conditions.
## sample3
4
3 5 1 2
3
1 2 3
5
-1 -3 -2 -4 -5
15
6
20
</p>