Suraj patwa

D. Almost All Divisors Solution
mail@pastecode.io avatar
unknown
c_cpp
2 months ago
885 B
3
Indexable
Never
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
 
using namespace std;
 
int main() {
    int t;
    cin >> t;
 
    while (t--) {
        int n;
        cin >> n;
 
        vector<ll> a(n);
        for(ll i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        
        long long num = (long long) a[0] * a[n-1];
        
        vector<long long> b;
        
        for(ll i = 2; i * i <= num; i++) {
            if(num % i == 0) {
                b.push_back(i);
                if(i != num / i) {
                    b.push_back(num / i);
                }
            }
        }
        sort(b.begin(), b.end());
        
        if(a == b) {
            cout << num << endl;
        } else {
            cout << -1 << endl;
        }
    }
 
    return 0;
}
Leave a Comment