Created
February 20, 2023 10:46
-
-
Save boysbee/c1f2a938949c5142d336518ef3fb07bc to your computer and use it in GitHub Desktop.
DiagonalDifference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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