import kotlin.math.max fun main(args: Array){ val numberCount = readLine()!!.toInt() val numberList = readLine()!!.trim().split(" ").map { it.toInt() } val primeList = getPrimeList() val numberMap = mutableMapOf() var stattIndex = 0 var ans = 0 for(i in primeList.indices) { primeList[i].toString().map { it.toString().toInt() }.forEach { val key = it numberMap[key]?.also { numberMap[key] = it+1 } ?: run { numberMap[key] = 1 } } while (numberMap.filter { it.value > 0 }.any { !numberList.contains(it.key) }) { primeList[stattIndex].toString().map { it.toString().toInt() }.forEach { numberMap[it] = numberMap[it]!!-1 } stattIndex++ } if(!numberMap.filter { it.value > 0 }.all { numberList.contains(it.key) }) { stattIndex = i + 1 } if(stattIndex <= i) { var startNum = 0 if(stattIndex == 0) { startNum = 1 } else { startNum = primeList[stattIndex-1] + 1 } var endNum = 5000000 if(i < primeList.lastIndex) { endNum = primeList[i + 1] - 1 } ans = Math.max(ans, endNum - startNum) } } if(ans == 0) { ans = -1 } println(ans) } fun getPrimeList():List { val max = 5000000 val primeList = mutableListOf() val flags = arrayOfNulls(max + 1) for(i in 2..max) { if(flags[i] != null && flags[i]!!) { continue } val maxIndex = max / i for(j in 1..maxIndex) { flags[i * j] = true } primeList.add(i) } return primeList }