#include
using namespace std;
int left_max(int [],int);
int right_max(int [],int ,int);
int min(int ,int );
int main()
{ int n;
cin>>n;
int arr[1000000];
int tw=0;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=1;i<n-1;i++)
{
int left_max1=left_max(arr,i);
int right_max1=right_max(arr,n,i);
tw+=min(left_max1,right_max1)-arr[i];
}
cout<<tw;
}
int left_max(int arr[],int i)
{
int maximum=0;
for(int j=0;j<=i;j++)
{
if(maximum<arr[j])
{
maximum=arr[j];
}
// maximum=max(maximum,arr[j]);
}
return maximum;
}
int right_max(int arr[],int x,int i)
{
int maximum=0;
for(int j=x-1;j>=i;j–)
{
if(maximum<arr[j])
{
maximum=arr[j];
}
// maximum=max(maximum,arr[j]);
}
return maximum;
}
int min(int lmax,int Rmax)
{
if(lmax>Rmax)
{
return Rmax;
}
return lmax;
}
Rain Water Problem test cases 6 7 8 is not working may you explain
The order of your time complexity is O(N*N) you have to do this problem in O(N) time complexity. I am attaching my personalised code, which you can take for reference and apply it in your 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.