Created
August 28, 2018 21:42
-
-
Save juanfurattini/c14912d7b06f2992ad394784f8f1bb24 to your computer and use it in GitHub Desktop.
This file contains 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
def solution(a) | |
head = a[0] | |
tail = a[1...a.size].reduce(&:+) | |
min = calc_diff(head, tail) | |
(1...a.size).each do |idx| | |
head += a[idx] | |
tail -= a[idx] | |
new_diff = calc_diff(head, tail) | |
min = new_diff if new_diff < min | |
end | |
min | |
end | |
def calc_diff(head, tail) | |
(head - tail).abs | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment