Skip to content

Commit 846a4a4

Browse files
authored
Merge pull request #482 from sumitvajarinkar/trapping-rain-water
closes #443 Trapping rain water
2 parents c37769e + 1f083f3 commit 846a4a4

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Trapping Rain Water
2+
//Approach 1
3+
// O(n^2) O(1)
4+
5+
//i/p: 3 0 1 2 5
6+
//o/p: 6
7+
8+
//Brute force approach
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
int traprain(int arr[],int n)
13+
{ // To store the maximum water
14+
// that can be stored
15+
int res = 0;
16+
17+
// i/p: 3 0 1 2 5
18+
//o/p: 6
19+
20+
// For every element of the array
21+
for (int i = 1; i < n-1; i++) {
22+
23+
// Find the maximum element on its left from o th to i th value
24+
int left = arr[i];
25+
for (int j=0; j<i; j++) //o(n^2)
26+
left = max(left, arr[j]);
27+
28+
// Find the maximum element on its right from j th th value to end
29+
int right = arr[i];
30+
for (int j=i+1; j<n; j++)
31+
right = max(right, arr[j]);
32+
33+
// Update the maximum water
34+
res = res + (min(left, right) - arr[i]); // 3 + 2 + 1
35+
}
36+
37+
return res;
38+
}
39+
int main()
40+
{
41+
int n;
42+
cout<<"Enter the number of elements in the array\n";
43+
cin>>n;
44+
int arr[n];
45+
cout<<"Enter the elements in the array\n";
46+
for(int i=0;i<n;i++)
47+
cin>>arr[i];
48+
cout<<"Total trapped water is "<<traprain(arr,n);
49+
return 0;
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Trapping Rain Water
2+
//Approach 2
3+
// O(n) O(2n)
4+
5+
//i/p: 3 0 1 2 5
6+
//o/p: 6
7+
8+
//Use 2 extra arrays
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
int getwater(int arr[],int n)
13+
{
14+
int res=0;
15+
int lmax[n],rmax[n];
16+
17+
//left max
18+
lmax[0]=arr[0];
19+
for(int i=1;i<n;i++)
20+
lmax[i]=max(arr[i],lmax[i-1]);
21+
22+
//starts from start to end
23+
//1st element as it is other max will place in lmax
24+
//Array is 5 0 6 2 3
25+
// |/\/\/\/\
26+
//lmax is 5 5 6 6 6
27+
28+
//right max
29+
rmax[n-1]=arr[n-1];
30+
for(int i=n-2;i>=0;i--)
31+
rmax[i]=max(arr[0],rmax[i+1]);
32+
33+
//starts from end to start
34+
//last element as it is other max will place in rmax
35+
//Array is 5 0 6 2 3
36+
// /\/\/\/\|
37+
//rmax is 6 6 6 3 3
38+
39+
//here we get result
40+
for(int i=1;i<n-1;i++)
41+
res+=(min(lmax[i],rmax[i])-arr[i]);
42+
43+
// min(lmax,rmax)-arr[i] min arr[i]
44+
// 5 5 6 6 6 5 5 6 3 3 - 5 0 6 2 3 = 0+5+0+1+0=6
45+
// 6 6 6 3 3
46+
47+
48+
return res;
49+
}
50+
int main()
51+
{
52+
int n;
53+
cout<<"Enter the number of elements in the array\n";
54+
cin>>n;
55+
int arr[n];
56+
cout<<"Enter the elements in the array\n";
57+
for(int i=0;i<n;i++)
58+
cin>>arr[i];
59+
cout<<"Total trapped water is "<<getwater(arr,n);
60+
return 0;
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Trapping Rain Water
2+
//Approach 3 (2 pointer approach)
3+
// O(n) O(1)
4+
5+
//i/p: 3 0 1 2 5
6+
//o/p: 6
7+
8+
/*
9+
We take 2 pointer left right
10+
and increment left and decrement right
11+
till we meet at a point
12+
*/
13+
14+
15+
#include<bits/stdc++.h>
16+
using namespace std;
17+
int getwater(int arr[],int n)
18+
{
19+
int left=0,right=n-1;
20+
int res=0;
21+
int leftmax=0,rightmax=0;
22+
23+
// i/p: 7 1 4 0 2 8 6 3 5
24+
//o/p: 23
25+
26+
while(left<=right)
27+
{
28+
//left side
29+
30+
if(arr[left]<=arr[right])
31+
{
32+
if(arr[left]>=leftmax)
33+
leftmax=arr[left];
34+
else
35+
res+=leftmax-arr[left]; // (7-1)=6 + (7-4)=3 + (7-0)=7 + (7-2)=5 ==> 21
36+
37+
left++; //increment
38+
}
39+
//right side
40+
else
41+
{
42+
if(arr[right]>=rightmax)
43+
rightmax=arr[right]; //5
44+
else
45+
res+=rightmax-arr[right]; //(5-3)=2 ==> 2
46+
right--; //decrement
47+
}
48+
}
49+
return res;
50+
}
51+
int main()
52+
{
53+
int n;
54+
cout<<"Enter the number of elements in the array\n";
55+
cin>>n;
56+
int arr[n];
57+
cout<<"Enter the elements in the array\n";
58+
for(int i=0;i<n;i++)
59+
cin>>arr[i];
60+
cout<<"Total trapped water is "<<getwater(arr,n);
61+
return 0;
62+
}

0 commit comments

Comments
 (0)