


Ninja has been given an array/list of integers ‘ARR’ of size ‘N’. Ninja has to find the greatest number present in the ‘ARR’ such that this number is also equal to the product of the two elements present in the ‘ARR’.
Note:
1. The pair should be from different indices.
2. If no such number is present in the ‘ARR’, then return -1.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single space-separated integer ‘N’ which represents the size of ‘ARR’.
The next line contains ‘N’ single space-separated integers representing the values of the ‘ARR’.
Output Format:
For each test case, print and integer denoting the greatest number present in the ‘ARR,’ such that the product of two elements present in the ‘ARR’ is equal to this element. Otherwise, print -1.
Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 50
2 <= ‘N’ <= 10^4
1 <= ‘ARR[i]’ <= 10^5
Time limit: 1 sec
2
5
1 2 3 4 5
5
2 2 10 10 4
5
4
In the first test case, the product of 5 and 1 is equal to 5, and 5 is also present in the ‘ARR’.
In the second test case, the product of 2 and 2 is equal to 4, and 4 is also present in the ‘ARR’.
2
5
2 3 4 5 9
4
10 2 20 8
-1
20
In the first test case, there is no pair present in the ‘ARR’ whose product is equal to an element that is present in the ‘ARR’.
In the second test case, the product of 10 and 2 is equal to 20, and 20 is also present in the ‘ARR’.
Can you think by finding all the pairs?
The basic idea is to find all the pairs and check if the product of two elements of a pair is equal to an element that is present in the ‘ARR’.
The steps are as follows:
O(N ^ 3), where ‘N’ is the size of ‘ARR’.
Because for finding our answer we are using three nested loops. Hence the time complexity is O(N ^ 3).
O(1)
Because we are not using any extra space for finding our answer.