Q:

kadane algorithm actual

# Python program to find maximum contiguous subarray 
  
def maxSubArraySum(a,size): 
      
    max_so_far =a[0] 
    curr_max = a[0] 
      
    for i in range(1,size): 
        curr_max = max(a[i], curr_max + a[i]) 
        max_so_far = max(max_so_far,curr_max) 
          
    return max_so_far 
  
# Driver function to check the above function  
a = [-2, -3, 4, -1, -2, 1, 5, -3] 
print"Maximum contiguous sum is" , maxSubArraySum(a,len(a)) 
  
#This code is contributed by _Devesh Agrawal_ 
6
int maxSubArraySum(int a[], int size) 
{ 
    int max_so_far = INT_MIN, max_ending_here = 0; 
  
    for (int i = 0; i < size; i++) 
    { 
        max_ending_here = max_ending_here + a[i]; 
        if (max_so_far < max_ending_here) 
            max_so_far = max_ending_here; 
  
        if (max_ending_here < 0) 
            max_ending_here = 0; 
    } 
    return max_so_far; 
}
3
//Kadene's algorithm where empty array is allowed.

int ms,cs;
	    ms=cs=0;
	    for(int i=0;i<n;i++)
	    {
	        cs=cs+a[i];
	        if(cs<0)
	        {
	            cs=0;
	        }
	        ms=max(ms,cs);
	}
	cout<<ms<<endl;
	}
0

New to Communities?

Join the community