RainWater Trapping problem

#include
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int main() {

int i,n,sum=0;
cin>>n;

int arr[n],arr1[n],arr2[n];

for(i=0;i<n;i++)
{
	cin>>arr[i];
}
arr1[n]=arr[0];

for(i=0;i<n;i++)
{
 	arr1[i]=max(arr1[i-1],arr[i]);
}
 arr2[n-1]=arr[n-1];
for(i=n-2;i>=0;i--)
{
	arr2[i]=max(arr2[i+1],arr[i]);
}

for(i=0;i<n;i++)
{
	sum+=min(arr1[i],arr2[i])-arr[i];
}

cout<<sum;
return 0;

}

tell the problem

@arsh_goyal at line 16 of your code arr1[0] should be equal to arr[0] and start your for loop from i=1.

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.