Skip to content

Instantly share code, notes, and snippets.

@johan--
Forked from peterc/equi.rb
Created October 20, 2013 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johan--/7068208 to your computer and use it in GitHub Desktop.
Save johan--/7068208 to your computer and use it in GitHub Desktop.
# http://codility.com/ for info
def equi(a)
pres, posts = 0, a.reduce(:+)
0.upto(a.length - 1) do |pivot|
pres += (pivot > 0 ? a[pivot-1] : 0)
posts -= a[pivot]
return pivot if pres == posts
end
-1
end
#puts equi([-7, 1, 5, 2, -4, 3, 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment