Nested for loop problem when i=0 and j=0

what is the value of cumsum[-1] when i=0.

@jindaldivish you cannot access negative indices of an array.

but we have accessed in the algorithms as we have written currentsum=cumsum[j]-cumsum[i-1]
and here i =0 for eg;

#include
using namespace std;
int main()
{
int n,current_sum=0,maximum_sum=0,left,right;
cin>>n;
int a[1000];
cin>>a[0];
int cumsum[1000];
cumsum[0]=a[0];
for(int i=1;i<n;i++)
{
cin>>a[i];
cumsum[i]=cumsum[i-1]+a[i];
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
current_sum=cumsum[j]-cumsum[i-1];
if(current_sum>maximum_sum)
{
maximum_sum=current_sum;
left=i;
right=j;
}
}
}
cout<<“maximum sum is: “<<maximum_sum<<endl;
for(int i=left;i<=right;i++)
cout<<a[i]<<”,”;
return 0;
}
what will happen when the nested for loops will execute for the first time with i=0 and j=0 because we dont have the value of cumsum[-1]

@jindaldivish
hello divish,
we cannot access negative index . it will give runtime error.
so when i=0 and j=0 ur current_sum=cumsum[j] only ( u need to handle this case separately)

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.