forked from Sahaj-Bamba/HacktoberTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeforces.cpp
More file actions
87 lines (77 loc) · 1.73 KB
/
codeforces.cpp
File metadata and controls
87 lines (77 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
long long int primeFactors(long long int n)
{
// Print the number of 2s that divide n
map<long long int ,long long int > mp;
while (n % 2 == 0)
{
//cout << 2 << " ";
mp[2]++;
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
//cout << i << " ";
mp[i]++;
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
mp[n]++;
long long int factor=1;
map<long long int ,long long int >::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
factor*=((*it).second+1);
}
return factor;
}
int main()
{
int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
long long int a[n];
map<long long int,long long int> mp;
set<long long int> st;
for(int i=0;i<n;i++)
{
cin>>a[i];
st.insert(a[i]);
}
sort(a,a+n);
int i=0;
int j=n-1;
long long int flag=0;
long long int temp=a[i]*a[j];
while(i<j)
{
if(temp!=(a[i]*a[j]))
flag=1;
i++;j--;
}
if(i==j&&(a[i]*a[j])!=temp)
flag=1;
if(flag==1)
cout<<"-1\n";
else
{
long long int sto;
if((primeFactors(temp))==(st.size()+2))
cout<<temp<<"\n";
else
cout<<"-1\n";
}
}
}