Diagonal Difference Solution in Kotlin -HackerRank

  • -100 < Elements in the matrix < 100
3 
11 2 4
4 5 6
10 8 -12
15

fun diagonalDifference(arr: Array<Array<Int>>): Int {
// Write your code here
var rightDiagonal = 0
var leftDiagonal = 0
for (x in 0 until arr.size) {
rightDiagonal += arr[x][arr.size-1-x]
leftDiagonal += arr[x][x]
}
return Math.abs(leftDiagonal - rightDiagonal) // integer value still positive}
11   
5
-12
   4   
5
10

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store