-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.kt
36 lines (26 loc) · 848 Bytes
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package warmup.diagonalDifference
import kotlin.math.abs
/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
fun diagonalDifference(arr: MutableList<List<Int>>): Int {
var leftToRightDiagonal = 0
var rightToLeftDiagonal = 0
for (i in arr.indices) {
leftToRightDiagonal += arr[i][i]
rightToLeftDiagonal += arr[i][(arr.size - 1) - i]
}
return abs(leftToRightDiagonal - rightToLeftDiagonal)
}
fun main() {
val n = readln().trim().toInt()
val arr = MutableList(n) { List(n) { 0 } }
for (i in 0 until n) {
arr[i] = readln().trimEnd().split(" ").map { it.toInt() }
}
val result = diagonalDifference(arr)
println(result)
}