


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’.
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.
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
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:
The basic idea of this approach is to store all values of the ‘ARR’ in a HashMap ‘ALL_VALUES’. Then sort the ‘ARR’.
Let's assume ‘ans’ = ‘a’ * ‘b’.
If ‘a’ and ‘b’ are equal then ‘ans’ = ‘a’ * ‘a’ or ‘ans’ = ‘a’ ^ 2.
So we can also check the second element of the pair till sqrt of ‘ans’.
The steps are as follows: