Skip to content

Commit 67e54dc

Browse files
committed
Trapping rain water using c++
1 parent d6eddaf commit 67e54dc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

trappingrainwater.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
int arr[]={1,8,6,2,5,4,8,3,7};
6+
int l=0,n=sizeof(arr)/sizeof(int),r=n-1;
7+
int max_area=0,h,a,b;
8+
9+
while(l<r){
10+
h=min(arr[l],arr[r]);
11+
b=r-l;
12+
a=h*b;
13+
max_area=max(a,max_area);
14+
if(arr[l]<=arr[r]){
15+
l++;
16+
}
17+
else{
18+
r--;
19+
}
20+
}
21+
cout<<"Maximum water that can be trapped is : "<<max_area;
22+
return 0;
23+
24+
}

0 commit comments

Comments
 (0)