def max_subarr(arr)
start_index, end_index = 0,
(0...arr.length).inject([arr.first]) do |max_sub, i|
(i...arr.length).each do |x|
if max_sub.inject(:+) < arr[i..x].inject(:+)
max_sub = arr[i..x]
start_index, end_index = i, x
end
end
max_sub
end
return [start_index, end_index]
end
max_subarr([98, -99, 198, -2, 950]) # => [2,4]
Could someone explain why we need the last max_sub
when we have the max_sub = arr[i..x]
in the nested loop? And, the function of inject([arr.first]) do |max_sub, i|