Skip to main content

Maximum Subarray

Definition​

The Maximum Subarray Algorithm is a method used to find the contiguous subarray within a one-dimensional array of numbers which has the largest sum

Practice​

maxSubarray(array):
max_sum = current_sum = array[0] // Initialize variables
for i from 1 to length(array)-1: // Iterate through the array
current_sum = max(array[i], current_sum + array[i]) // Update current sum
max_sum = max(max_sum, current_sum) // Update max sum
return max_sum // Return the maximum sum found