Skip to content

Instantly share code, notes, and snippets.

@samstokes
Created January 8, 2010 21:05
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 samstokes/272412 to your computer and use it in GitHub Desktop.
Save samstokes/272412 to your computer and use it in GitHub Desktop.
Solution to Codility demo problem, finding equilibrium index of an array
def sums_before(src)
dest = [0]
src.each_with_index do |item, index|
dest[index + 1] = dest[index] + item
end
dest
end
def equi ( arr )
sums_left = sums_before(arr)
# p sums_left
arr.reverse!
sums_right = sums_before(arr); sums_right.reverse!
# p sums_right
arr.each_with_index do |item, index|
if sums_left[index] == sums_right[index + 1]
return index
end
end
return -1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment