Max Sum Contiguous Subarray c++ best solution

google
amazon
facebook
Tags: #<Tag:0x00007f1827bfb990> #<Tag:0x00007f1827bfb850> #<Tag:0x00007f1827bfb4e0>

#1
int maxso=INT_MIN,maxend=0;
for(int i:A)
{
    maxend+=i;
    if(maxend>maxso)
    maxso=maxend;
    
    if(maxend<0)
    maxend=0;
    
}
return maxso;

#2

Can you please explain this code logic


#3

@joshisweta2000_f8cdf It’s just cleanly implemented Kadane’s algorithm


#4

@joshisweta2000_f8cdf thx for pointing out Kadane’s algorithm. Above solution looks like iterative approach of Kadane’s. here is a really good explanation of Kadane’s algorithm https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d