educative.io

Educative

Solution Review: Maximum Sum Sublist does not work in some test cases

def find_max_sum_subarray(lst):
if (len(lst) < 1):
return 0;

curr_max = lst[0];
global_max = lst[0];
length_array = len(lst);
for i in range(length_array):
if curr_max < 0:
curr_max = lst[i]
else:
curr_max += lst[i]
if global_max < curr_max:
global_max = curr_max

return global_max;

lst = [25, 2, -5, 1, 2, 3, 6, -5, 1];
print("Sum of largest subarray: ", find_max_sum_subarray(lst));

Above code is mentioned in the solution. But it does not work for the test case I have mentioned above. It is giving me answer as 59 where as real answer should be 30.

Hi @Abhiram_Pattarkine,

The for loop should start from 1 and iterate till the length of the list, i.e., for i in range(1, length_array):. This has been fixed in the lesson as well. Also, the answer to your mentioned list should be 34, i.e., 25+2-5+1+2+3+6=34.

Happy Learning!

Anum Hassan|Developer Advocate
Educative.io

Thank you.

You are most welcome @Abhiram_Pattarkine. Happy to help! :slight_smile: