Skip to content

Instantly share code, notes, and snippets.

@boysbee
Created February 20, 2023 10:46
Show Gist options
  • Save boysbee/c1f2a938949c5142d336518ef3fb07bc to your computer and use it in GitHub Desktop.
Save boysbee/c1f2a938949c5142d336518ef3fb07bc to your computer and use it in GitHub Desktop.
DiagonalDifference
// https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true
import java.math.BigDecimal
import kotlin.math.abs
import kotlin.math.absoluteValue
val arr = arrayOf(
arrayOf(1, 2, 3),
arrayOf(4, 5, 6),
arrayOf(9, 8, 9),
)
fun diagonalDifference(arr: Array<Array<Int>>): Int {
return (arr.foldIndexed(0) { index, acc, row ->
acc + row[index]
} - arr.foldIndexed(0) { index, acc, row ->
acc + row[(arr.size - 1) - index]
}).absoluteValue
}
diagonalDifference(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment