Getting error in the cumulative sum code when the starting index for the outer loop starts from 0

for (int i = 0; i < size; ++i)
{
for (int j = i; j < size; ++j)
{
int currentSum = cumulativeSum[j] - cumulativeSum[i-1];
if (currentSum > maximumSum)
{
maximumSum = currentSum;
startIndex = i;
endIndex = j;
}
}
}

here cumulativeSum[i-1] for i = 0 gives wrong answer. Please helf how to resolve this

hello @mangeshjsh78
handle this case separately by adding an if statement.

if(i==0)
currentsum=cumulativesum[j]

@aman212yadav Thank you. It fixed the code.

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.