Open minimanimoh opened 3 years ago
You have been given an array containing numbers. Find and return the largest sum in a contiguous subarray within the input array.
Example 1:
arr= [1, 2, 3, -4, 6]
8
Example 2:
arr = [1, 2, -5, -4, 1, 6]
7
def max_sum_subarray(arr): max_sum = arr[0] current_sum = arr[0] for num in arr[1:]: current_sum = max(current_sum + num, num) max_sum = max(current_sum, max_sum) return max_sum
Problem Statement
You have been given an array containing numbers. Find and return the largest sum in a contiguous subarray within the input array.
Example 1:
arr= [1, 2, 3, -4, 6]
8
, which is the sum of all elements of the array.Example 2:
arr = [1, 2, -5, -4, 1, 6]
7
, which is the sum of the last two elements of the array.