Skip to content

Instantly share code, notes, and snippets.

@jakl
Created September 6, 2013 22:12
Show Gist options
  • Save jakl/6470666 to your computer and use it in GitHub Desktop.
Save jakl/6470666 to your computer and use it in GitHub Desktop.
Silly little code practice from last week
#!/usr/bin/env ruby
# Find the index(es) in an array of integers where sums above and below are equal
def solution(a)
sum = a.reduce(:+)
return -1 unless sum
return 0 if sum - a[0] == 0
i_sum = 0
(1...a.size).each do |i|
i_sum += a[i-1]
if sum - i_sum - a[i] == i_sum
return i
end
end
-1
end
puts solution([-1,0,1]*2_000_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment