Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created August 19, 2015 05:55
Show Gist options
  • Save feliperazeek/463a8aa85a0d65b032e3 to your computer and use it in GitHub Desktop.
Save feliperazeek/463a8aa85a0d65b032e3 to your computer and use it in GitHub Desktop.
import scala.math.{min, abs}
object Solution {
def solution(A: Array[Int]): Int = {
if (A.size < 2 || A.size > 100000) sys.error(s"Invalid input - array size: ${A.size}")
val total = A.map(_.toLong).sum
(A.foldLeft[(Int, Long, Long)](-1, -1, 0l) { (t, i) =>
if (i < -1000 || i > 1000) sys.error(s"Invalid array element: $i")
val (x, currentMin, lastLeftSum) = t
val index = x + 1
(index + 1 == A.size) match {
case true =>
// Do nothing on the last element
t
case false =>
val leftSum = lastLeftSum.toLong + A(index).toLong
val rightSum = total - leftSum
val thisMin = abs(leftSum- rightSum)
val results = if (currentMin == -1) thisMin
else min(currentMin, thisMin)
(index, results, leftSum)
}
})._2.toInt
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment