Maximum subarray 2

In this part sir uses to generate all sub arrays using two loops

for(int i= 0,i<n ; I++)
{
for(int j=i ;j<n;j++){
curr_sum= csum[ j] -csum[i-1];
}
}

When i=0 and j=0 then csum[i-1] becomes csum[-1] so it gives error because csum[-1] not exist.

yes this may or may not give error depend on compiler
if the memory just before the array is free then it will not give error otherwise give error
chances of run time error are high
for me it is giving error

solution: use if else

if(i>0)curr_sum= csum[ j] -csum[i-1];
else curr_sum= csum[ j]

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.