What if all the elements in an array are negative values?

input : -2 -1 -3 -5 -6

in this case. the o/p would be -1 as the largest sum subarray. Isn’t it? But Kadane will give 0 as max_val would be 0 always

hello @amandeepdogra65

initilaise max_val with any value that is present in ur array .
and then it will work fine for every cases.

I didn’t get.
Suppose I initilise max_val=arr[0] and arr[0] is -2, in the first iteration itself max_val will become 0 when we will choose max among max_val and curr_sum. Isn’t it?

how u r implemnting kadane?

try this->

max_val=a[0];
cur=0;
for i -> 0 ....n-1:
     cur+=a[i]
     if cur > max_val
          max_val=cur
    if cur<0
           cur=0

i got it. Thanks man!