Level
Find the error
-
def max_subarray(nums):
-
    if not nums:
-
        return 0
-
    current_sum = max_sum = nums[0]
-
    for num in nums[1:]:
-
        current_sum = current_sum + num
-
        max_sum = max(max_sum, current_sum)
-
    return max_sum
-
print(max_subarray([-2, 1, -3, 4, -1, 2, 1, -5, 4]))

LeetCode 53: Maximum Subarray

In this LeetCode-style problem, you need to implement Kadane's algorithm to find the maximum sum of a contiguous subarray. Identify the line with a logical error and select the correct implementation to fix it. Pay attention to how the current sum is updated in each iteration and how it affects the overall maximum sum.