Created
September 6, 2013 22:12
-
-
Save jakl/6470666 to your computer and use it in GitHub Desktop.
Silly little code practice from last week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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