Skip to content

Instantly share code, notes, and snippets.

@andrewsafwatsamuel
Created September 17, 2020 19:21
Show Gist options
  • Save andrewsafwatsamuel/80e8ac6c244bb0b91c1d3e0cbee6bfc9 to your computer and use it in GitHub Desktop.
Save andrewsafwatsamuel/80e8ac6c244bb0b91c1d3e0cbee6bfc9 to your computer and use it in GitHub Desktop.
fun hourglassSum(arr: Array<Array<Int>>): Int {
val values = arrayListOf<Int>()
for (i in 0 until (arr.size - 2)) {
for (j in 0 until (arr[i].size - 2)) {
val current = arr[i]
val currentP1 = arr[i + 1]
val currentP2 = arr[i + 2]
val sum = current[j] + current[j + 1] + current[j + 2] + currentP1[j + 1] + currentP2[j] + currentP2[j + 1] + currentP2[j + 2]
values.add(sum)
}
}
return values.max()?:0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment