LeetCode in Kotlin

3770. Largest Prime from Consecutive Prime Sum

Medium

You are given an integer n.

Return the largest prime number less than or equal to n that can be expressed as the sum of one or more consecutive prime numbers starting from 2. If no such number exists, return 0.

Example 1:

Input: n = 20

Output: 17

Explanation:

The prime numbers less than or equal to n = 20 which are consecutive prime sums are:

The largest is 17, so it is the answer.

Example 2:

Input: n = 2

Output: 2

Explanation:

The only consecutive prime sum less than or equal to 2 is 2 itself.

Constraints:

Solution

class Solution {
    fun largestPrime(n: Int): Int {
        var i = 0
        var j: Int = ppr.size - 1
        var r = 0
        while (i <= j) {
            val m = (i + j) shr 1
            if (ppr[m] <= n) {
                r = ppr[m]
                i = m + 1
            } else {
                j = m - 1
            }
        }
        return r
    }

    companion object {
        var ppr: IntArray = intArrayOf(
            2, 5, 17, 41, 197, 281, 7699, 8893, 22039, 24133, 25237, 28697, 32353, 37561, 38921, 43201,
            44683, 55837, 61027, 66463, 70241, 86453, 102001, 109147, 116533, 119069, 121631, 129419,
            132059, 263171, 287137, 325019, 329401, 333821, 338279, 342761, 360979, 379667, 393961,
            398771,
        )
    }
}