Max contiguous slice in Ruby 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]  &lt; 0 ) then
      curr_start = i+1
      curr_sum = 0
    elsif (curr_sum + a[i] &gt; curr_sum) then  
      curr_sum = curr_sum + a[i]
      curr_end = i
      if (curr_sum &gt; largest_sum) then
        max_start, max_end = curr_start,curr_end
        largest_sum = curr_sum
      end 
    end
  end
  a[max_start..max_end]
end


p max_slice([-3,39,2,-1,65]) #&gt; [39, 2, -1, 65]
p max_slice([12,-3,39,2,-1,65]) #&gt; [12, -3, 39, 2, -1, 65]
p max_slice([]) #&gt; []
p max_slice([2,3]) #&gt; [2, 3]
p max_slice([-2,3]) #&gt; [3]
p max_slice([-2]) #&gt; [-2]
p max_slice([12,-3,39,2,-1,102]) #&gt; [12, -3, 39, 2, -1, 102]
p max_slice([-5,-3,39,2,-39,65]) #&gt; [39, 2, -39, 65]
p max_slice([-5,5,39,2,-60,65]) #&gt; [65]
p max_slice([-5,35,39,2,-60,65]) #&gt; [35, 39, 2, -60, 65]
p max_slice([-5,35,39,2,-160,65]) #&gt; [35, 39, 2]
What you are doing here is Kadane's algorithm. Wikipedia presents an implementation in Python which should not be hard to rewrite in Ruby.