how to get the maximum sum using this algorithm if all the array elements are negative?
Problem in logic CPP - Maximum Subarray Sum 3 (Kadane's Algo)
Hi sankalandasgupta
using the following code we can get max sum if all are negative elements :
int curr_sum=arr[0], max_sum=arr[0];
for(int i=1;i<n;i++){
if(curr_sum+arr[i]<arr[i]){
curr_sum=arr[i];
}
else{
curr_sum+=arr[i];
}
max_sum=max(max_sum,curr_sum);
}
cout<<max_sum;
Hope it helps
Mark resolved if satisfied