Split an array into two non-empty parts and maximize the smaller of the two part gcds.
Medium7Number theoryMathBrute forceGreedyNo attempts yetTime limit2sMemory limit256 MBPeter has just learned what the greatest common divisor of a set of integers is. He likes the idea so much that now he looks for it in everything he sees.
During today's computer science lesson the teacher wrote an array of integers on the blackboard, and Peter noticed that its elements can be divided into two parts M1 and M2 so that gcd(M1) and gcd(M2) are both quite big. Here gcd(M) is the greatest common divisor of all numbers in M.
Peter has decided to generalize the problem. For a given array, divide all of its elements into two non-empty parts M1 and M2, with every element going into exactly one part, so that min(gcd(M1),gcd(M2)) is as large as possible. Print that largest value.
The input contains several tests. The first line contains the number of tests t (1≤t≤1000).
Each test is given on two lines. The first line contains an integer n (2≤n≤5×104), the size of the array. The second line contains n integers ai (1≤ai≤109), the elements of the array.
The sum of the values of n over all tests in one input does not exceed 5×104.
For each test print one integer on its own line: the largest possible value of min(gcd(M1),gcd(M2)).