LeetCode in Kotlin

2614. Prime In Diagonal

Easy

You are given a 0-indexed two-dimensional integer array nums.

Return the largest prime number that lies on at least one of the diagonals of nums. In case, no prime is present on any of the diagonals, return 0.

Note that:

In the above diagram, one diagonal is [1,5,9] and another diagonal is [3,5,7].

Example 1:

Input: nums = [[1,2,3],[5,6,7],[9,10,11]]

Output: 11

Explanation: The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.

Example 2:

Input: nums = [[1,2,3],[5,17,7],[9,11,10]]

Output: 17

Explanation: The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.

Constraints:

Solution

class Solution {
    fun diagonalPrime(nums: Array<IntArray>): Int {
        var i = 0
        var j = nums[0].size - 1
        var lp = 0
        while (i < nums.size) {
            val n1 = nums[i][i]
            if (n1 > lp && isPrime(n1)) {
                lp = n1
            }
            val n2 = nums[i][j]
            if (n2 > lp && isPrime(n2)) {
                lp = n2
            }
            i++
            j--
        }
        return lp
    }

    private fun isPrime(n: Int): Boolean {
        if (n == 1) {
            return false
        }
        if (n == 2 || n == 3) {
            return true
        }
        var i = 2
        while (i <= Math.sqrt(n.toDouble())) {
            if (n % i == 0) {
                return false
            }
            i++
        }
        return true
    }
}