In the code
for(int i=0;i<n;i++){
for(int j=i;j<n;j++)}
currentSum=0;
currentSum+=cumSum[j] - cumSum[i-1];
if(currentSum>maximumSum){
maximumSum=currentSum;
left=i;
right=j;
}
}
}
How is this code working for i=0?
Because for i=0 , cumSum[i-1] will be cumSum[-1].
Hence the code should not give the correct answer.
Array out of bounds
@D18PYTBDWK0002 your first loop should start from i=1 to n because you have to capture zero index of cumSum[0]=a[0];
make change and it will work
CumSum has already been created. Here, we’re finding sum between i and j using the logic currentSum = cumSum[j] - cumSum[i-1].
Prateek bhaiya has done this in lecture and it worked correctly but it should not work for i=1. Right?
@D18PYTBDWK0002 hey satyam it work because when i=1 then we are calculating cumSum[j]-cumSum[i-1];
here j=1 and i=i-1; i=1-1;
so cumSum[1]-cumSum[0];
hope you will understand don’t confuse with loop
Hey Satyam,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.
Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.