Why Is this code showing run error on test cases 7 and 8. Where’s the problem?
Run error in 2 test cases
Paras, pls share your code by saving on ide so that I could check it …
There is a slight error in your logic… Pls use the following logic as :
long long int sum=0;
long long int left[1000000];
for(int i=0; i<n;i++)
{
left[i]=max(a[i],left[i-1]);
}
long long int right[1000000];
right[n-1]=a[n-1];
for(int i=n-2;i>=0;i–)
{
right[i]=max(a[i],right[i+1]);
}
long long int water[1000000];
for(int i=0; i<n; i++)
{
water[i]= min(left[i],right[i])-a[i];
}
for(int i=0; i<n; i++)
{
sum= sum+ water[i];
}
cout<<sum;
}