Skip to content

Instantly share code, notes, and snippets.

@fehmicansaglam
Created April 28, 2015 10:48
Show Gist options
  • Save fehmicansaglam/e83801d98eea1108978e to your computer and use it in GitHub Desktop.
Save fehmicansaglam/e83801d98eea1108978e to your computer and use it in GitHub Desktop.
How to search for an element in an array while passing state for the next iteration
val numbers = readLine().split(" ").map(_.toInt) // an array of numbers
val leftSums = numbers.scan(0)(_ + _).init // calculate cumulative sums from left
// search from right to find the same sum from left
var sum = 0
val result = numbers.zipWithIndex.reverse.find { case (number, index) =>
if (sum == leftSums(index)) true
else {
sum = sum + number
false
}
}
@fehmicansaglam
Copy link
Author

@metanet my answer for @korayal applies to you. And also this: https://twitter.com/fehmicans/status/593039964268552192

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment