Kadane's algorithm

for determining the maximum sum of contiguous subarrays


Unlike the algorithm for determining the maximum sum of subarrays according to the definition of a problem that is slow and has quadratic complexity, Kadane's algorithm is faster because it is linear complexity.
Suppose we have the following array of integers:​

{3,5,-10,-34,16 2}


​Determining the maximum sums according to Kadan's algorithm would be done as follows:
#include < stdio.h >
#include < stdlib.h >


int main()
{
int A[]={ 3,5,-10,-34,16 2};
int n= sizeof(niz)/sizeof(A[0]); //size of array
int MaxCurrent=0; //the current maximum after the end of the iteration of the outer loop
int Max=0; //maximum sum from beginning
/* Through the outer loop we move the current position for the elements of the array from the first to the last */
for(int currentInd=0; currentInd< n; currentInd++)
{
MaxCurrent=MaxCurrent+A[currentInd]; //We create the sum for the current position

if(MaxCurrent>Max)
Max=MaxCurrent; // if the current maximum is higher after the iterations are completed, the existing maximum is updated
if(MaxCurrent < 0)
/* In case we get a negative number for the current maximum, we use the sum of the empty subarray, which is 0 */
MaxCurrent = 0;
}
printf("Maximum sum of contiguous arrays is %3d", Max);
return 0;
}
Maximum sum of subarray