code that bhaiya has explained , which is not working in case of all -ve element
Maximum subarray sum
@sk3657860 yes you have to check that case manually . its the drawback of kadane algorithm .
hope its clear if yes dont forget to hit like and mark resolved
1 Like
int cs=arr[0]>0?arr[0]:0;
int ms=arr[0];
for(int i=0;i<3;i++){
cs+=arr[i];
ms=max(ms,cs);
cs=max(0,cs);
}
i have tried , it’s working
is it fine ?
or anything i am missing ?
int arr[]={-3,-2,-5};
@sk3657860 when all negative ans will be max element in the array in this case -2 . no need to do anything just print max element
1 Like
ooh great thank you so much