Trapping Rain Water

question–>
https://practice.geeksforgeeks.org/problems/trapping-rain-water/0#
code–>

#include <iostream>
#include<climits>
using namespace std;
int fun(int* arr,int n){
    int *left=new int[n+1];
    for(int i=0;i<n;i++){
        left[i]=0;
        
    }
    left[0]=arr[0];
    int maxi=INT_MIN;
    
    for(int i=1;i<n;i++){
       maxi=max(maxi,arr[i]);
       left[i]=maxi;
    }
    int *right=new int[n+1];
    for(int i=0;i<n;i++){
        right[i]=0;
        
    }
    maxi=INT_MIN;
    right[n-1]=arr[n-1];
    for(int i=n-2;i>=0;i--){
        maxi=max(maxi,arr[i]);
        right[i]=maxi;
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(min(left[i],right[i])-arr[i]>=0){
            ans+=min(left[i],right[i])-arr[i];
        }
    }
    return ans;
}
int main() {
	//code
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    int *arr=new int[n+1];
	    for(int i=0;i<n;i++){
	        cin>>arr[i];
	    }
	   cout<<fun(arr,n)<<endl;
	}
	return 0;
}

not passing all testcases

Hey @sheikhhaji18 happy new year :slight_smile:
Try to submit this code. And let me know if this gets accepted or not.

Happy new year @mr.encoder
i should have made left[0]=0;
right[n-1]=0;
that’s why it is not passing now it passing all testcases

Nice bro, you can follow my this github repo as i will add on many problems solution in it. With well documentation too. Have a great year ahead bro :slight_smile:

thanks bro looking forward @mr.encoder thanks for making it public

1 Like

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.