Created
May 26, 2023 14:44
-
-
Save iafsilva/0d79067e83e0fa9220118098acf976f0 to your computer and use it in GitHub Desktop.
TapeEquilibrium
This file contains 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
fun solution(a: IntArray): Int { | |
val nrSplits = a.size - 1 | |
val differences = IntArray(nrSplits) | |
var leftTape = 0 | |
var rightTape = a.sum() | |
var x: Int | |
for (p in 1..nrSplits) { | |
x = a[p-1] | |
leftTape += x | |
rightTape -= x | |
differences[p-1] = abs(leftTape - rightTape) | |
} | |
// get min split diff | |
return differences.minOrNull()!! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment