Diagonal Difference

  • + 0 comments

    Any Kotlin guys here? I'm trying to use the same logic here, but seems like always getting Runtime Error all the time. Could somebody tell me what is wrong here?

    fun main(args: Array<String>) {
    
        val numInputs = readLine()!!.toInt()
        var leftD = 0
        var rightD = 0
    
        for (j in 0..numInputs) {
            for (k in 0..numInputs) {
                val curInput = readLine()!!.toInt()
                if (j == k) {
                    leftD += curInput
                }
                if (j+k == (numInputs - 1)) {
                    rightD += curInput
                }
            }
        }
    
        println(Math.abs(leftD - rightD))
    }