Skip to content

Instantly share code, notes, and snippets.

@iafsilva
Created May 26, 2023 14:44
Show Gist options
  • Save iafsilva/0d79067e83e0fa9220118098acf976f0 to your computer and use it in GitHub Desktop.
Save iafsilva/0d79067e83e0fa9220118098acf976f0 to your computer and use it in GitHub Desktop.
TapeEquilibrium
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