Skip to content

Instantly share code, notes, and snippets.

@juanfurattini
Created August 28, 2018 21:42
Show Gist options
  • Save juanfurattini/c14912d7b06f2992ad394784f8f1bb24 to your computer and use it in GitHub Desktop.
Save juanfurattini/c14912d7b06f2992ad394784f8f1bb24 to your computer and use it in GitHub Desktop.
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