Find The Number FTNUM SOLUTIONS BRBG2020 Code Chef

Find The Number FTNUM SOLUTIONS BRBG2020 Code Chef

A prime number is number x which has only divisors as 1 and x itself. Harsh is playing a game with his friends, where his friends give him a few numbers claiming that they are divisors of some number x but divisor 1 and the number x itself are not being given as divisors. You need to help harsh find which number’s divisors are given here.
 
His friends can also give him wrong set of divisors as a trick question for which no number exists.
 
Simply, We are given the divisors of a number x ( divisors except 1 and x itself ) , you have to print the number if only it is possible. You have to answer t queries. (USE LONG LONG TO PREVENT OVERFLOW)
 
Input:
First line is T queires.
Next are T queries.
First line is N ( No of divisors except 1 and the number itself )
Next line has N integers or basically the divisors.
Output:
Print the minimum possible x which has such divisors and print -1 if not possible.
 
Constraints
1<= T <= 30
1<= N <= 350
2<= Di <=10^6
Sample Input:
    3
    2
    2 3
    2
    4 2
    3
    12 3 2
Sample Output:
    6
    8
    -1
EXPLANATION:
Query 1 : Divisors of 6 are ( 1,2,3,6) Therefore, Divisors except 1 and the number 6 itself are ( 2 , 3). Thus, ans = 6. Query 2 : Divisors of 8 are ( 1,2,4,8) Therefore, Divisors except 1 and the number 8 itself are ( 2 , 4). Thus, ans = 8. Query 3 : There is no such number x with only ( 1,2,3,12,x ) as the divisors.
 
                                                      Download the Solution
 
SOLUTION: #code in python3.6
 
def fun(l, z):
    l.sort()
    x = l[0]*l[z-1]
    temp = []
    i = 2
    while(i * i <= x):
        if(x % i == 0):
            temp.append(i)
            if ((x//i) != i):
                temp.append(x//i)
        i += 1
    temp.sort()
    if(len(temp) != z):
        return -1
    else:
        j = 0
        for k in range(z):
            if(temp[j] != temp[k]):
                return -1
            else:
                j += 1
    return x
for i in range(int(input())):
  n = int(input())
  arr = list(map(int,input().split(‘ ‘)))
  print(fun(arr,n))
 

Leave a Comment