![JAR search and dependency download from the Maven repository](/logo.png)
g2701_2800.s2761_prime_pairs_with_target_sum.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2701_2800.s2761_prime_pairs_with_target_sum
// #Medium #Array #Math #Enumeration #Number_Theory
// #2023_08_10_Time_537_ms_(100.00%)_Space_54.2_MB_(46.15%)
class Solution {
fun findPrimePairs(n: Int): List> {
val answer: MutableList> = ArrayList()
for (a in list) {
val other = n - a
if (other < n / 2 || a > n / 2) break
if (primes.contains(other)) answer.add(listOf(a, other))
}
return answer
}
companion object {
private val primes: HashSet = HashSet()
private val list: MutableList = ArrayList()
init {
val m = 1000001
val visited = BooleanArray(m)
for (i in 2 until m) {
if (!visited[i]) {
primes.add(i)
list.add(i)
var j: Int = i
while (j < m) {
visited[j] = true
j += i
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy