Problem:
Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.

Solution in Kotlin (1.0.0) (judge verdict: Wrong Answer)

val gcdMem:MutableMap<Pair<Int, Int>, Int> = mutableMapOf()

fun main(args: Array<String>) {
    val n = Integer.parseInt(readInputLine())
    val vas = readInputLine().split(" ").map { it -> Integer.parseInt(it) }
    
    var gcds: MutableList<Int> = mutableListOf()

    // すべての隣接する2数のgcdを求める
    for (i in 0..(n-1)) {
        if (i != n - 1) {
            gcds.add(gcdFirst(vas[i], vas[i + 1]))
        } else {
            gcds.add(gcdFirst(vas[i], vas[0]))
        }
    }
    
    // 求めたすべてのgcd配列の隣接するgcdをすべて求める。
    // 要素が3以下となった際に最も大きい値が求める値。
    while (gcds.size > 3) {
        val tmp = gcds.toList()
        val size = tmp.size
        gcds.clear()
        
        for (i in 0..(size - 1)) {
            if (i != size - 1) {
                gcds.add(gcdFirst(tmp[i], tmp[i + 1]))
            } else {
                gcds.add(gcdFirst(tmp[i], tmp[0]))
            }
        }
    }

    println(gcds.sortedDescending()[0])
}
 
fun readInputLine(): String {
    return readLine()!!
}

// gcdのトップ呼び出し関数
fun gcdFirst(a: Int, b: Int): Int {
    if (a >= b) {
        return gcdMem.getOrElse(Pair(a, b)) {
            val res = gcd(a, b)
            gcdMem[Pair(a, b)] = res
            
            res
        }
    } else {
        return gcdMem.getOrElse(Pair(b, a)) {
            val res = gcd(b, a)
            gcdMem[Pair(b, a)] = res
            
            res
        }
    }
}

// gcdの実計算関数
tailrec fun gcd(a: Int, b: Int): Int {
    // a >= b とする。
    if(b == 0) {
        return a
    }
    return gcd(b, a % b)
}