import kotlin.math.max fun main(args: Array){ val numberCount = readLine()!!.toInt() val numberList = readLine()!!.trim().split(" ").map { it.toInt() } val primeList = getPrimeList() val primeSplitedList = primeList.map { it.toString().map { it.toString().toInt() } } val numberMap = mutableMapOf() (0..9).forEach{numberMap[it] = 0} var stattIndex = 0 var ans = 0 for(i in primeList.indices) { primeSplitedList[i].forEach {numberMap[it] = numberMap[it]!! + 1} while (numberMap.filter { it.value > 0 }.keys.any { !numberList.contains(it) }) { primeSplitedList[stattIndex].forEach { numberMap[it] = numberMap[it]!!-1 } stattIndex++ } if(stattIndex <= i && numberList.all { numberMap.filter { it.value > 0 }.keys.contains(it) }) { var startNum = 0 startNum = if(stattIndex == 0) { 1 } else { 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 }