Skip to content

Instantly share code, notes, and snippets.

@metallurgix
Created December 8, 2014 17:59
Show Gist options
  • Save metallurgix/62e661cf14d1c77c2704 to your computer and use it in GitHub Desktop.
Save metallurgix/62e661cf14d1c77c2704 to your computer and use it in GitHub Desktop.
Codility-Lesson 1-Challenge 1
def solution(a)
if a.empty?
0
elsif a.length==2
a[1]-a[0]
else
right=a.inject(:+)-a[0]
left=a[0]
min=(right-left).abs
(1..a.length-1).each do |i|
right-=a[i]
left+=a[i]
if (left-right).abs<min
min=(left-right).abs
end
end
min
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment