I need to compute the max contiguous slice in an array. I wrote this function, but I am not sure if it is correct or I am missing edge cases. I ran several cases.def max_slice(a)  max_start,max_end = 0,0  largest_sum = 0  curr_start,curr_end = 0,0  curr_sum = 0  for i in 0..a.length-1     if (curr_sum + a[i]  < 0 ) then      curr_start = i+1      curr_sum = 0    elsif (curr_sum + a[i] > curr_sum) then        curr_sum = curr_sum + a[i]      curr_end = i      if (curr_sum > largest_sum) then        max_start, max_end = curr_start,curr_end        largest_sum = curr_sum      end     end  end  a[max_start..max_end]endp max_slice([-3,39,2,-1,65]) #> [39, 2, -1, 65]p max_slice([12,-3,39,2,-1,65]) #> [12, -3, 39, 2, -1, 65]p max_slice([]) #> []p max_slice([2,3]) #> [2, 3]p max_slice([-2,3]) #> [3]p max_slice([-2]) #> [-2]p max_slice([12,-3,39,2,-1,102]) #> [12, -3, 39, 2, -1, 102]p max_slice([-5,-3,39,2,-39,65]) #> [39, 2, -39, 65]p max_slice([-5,5,39,2,-60,65]) #> [65]p max_slice([-5,35,39,2,-60,65]) #> [35, 39, 2, -60, 65]p max_slice([-5,35,39,2,-160,65]) #> [35, 39, 2]
Max contiguous slice in Ruby
algorithm;ruby;array;dynamic programming