Here is my quick solution to your problem =)
Separate the challenges to understand what I'm doing =)
def max_subsum(a)
(0...a.length).flat_map { |i| (i...a.length).map { |j| i..j } }.inject([a[0], 0..0]) { |max, i| a[i].inject(:+) > max.first ? [a[i].inject(:+),i ]: max }.last
end
Output:
max_subsum([100, -101, 200, -3, 1000])
=> 2..4
max_subsum([1, 2, 3])
=> 0..2
You can convert to an array. I was not worried since I like the range =)
, , - , : -)
:
(0...a.length).flat_map { |i| (i...a.length).map { |j| i..j } }
. .
[a[0], 0..0] , : a[0] , 0..0 . max flat_map, , max.