Skip to content

Instantly share code, notes, and snippets.

@mrdougwright
Created July 29, 2014 00:16
Show Gist options
  • Save mrdougwright/5640c2124efeb36d2c50 to your computer and use it in GitHub Desktop.
Save mrdougwright/5640c2124efeb36d2c50 to your computer and use it in GitHub Desktop.
Max Slice
def max_slice(arr)
arr.size.times do |x|
if sum(arr[x..-1]) >= sum(arr)
arr.shift
end
if sum(arr[x..-2]) > sum(arr)
arr.pop
end
end
p arr
end
def sum(arr)
arr = Array(arr)
arr.inject(0) { |sum, x| sum + x.to_i }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment