Advertisement
dcramer

TapeEquilibrium Scala

Dec 14th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.48 KB | None | 0 0
  1. object Solution {
  2.  
  3.     def solution(A: Array[Int]): Int = {
  4.       def recurse(smallestResult: Int, headSum: Int, tailSum: Int, restOfList: List[Int]): Int = {
  5.         if(restOfList.isEmpty) smallestResult
  6.         else recurse(math.min((headSum - tailSum).abs,smallestResult),headSum + restOfList.head,tailSum - restOfList.head,restOfList.tail)
  7.       }
  8.       val initialTailSum = A.tail.sum
  9.       recurse((A.head - initialTailSum).abs, A.head, initialTailSum, A.tail.toList)
  10.     }
  11.  
  12. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement