forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrappedWater.cpp
48 lines (44 loc) · 894 Bytes
/
TrappedWater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
//find left array (big building at left)
int left[n];
int temp = 0;
for (int i = 0; i < n; i++)
{
if (temp < arr[i])
{
temp = arr[i];
}
left[i] = temp;
// cout<<left[i]<<" ";
}
//find right array (big building right)
int right[n];
temp = 0;
for (int i = (n-1); i >= 0; --i)
{
if (temp < arr[i])
{
temp = arr[i];
}
right[i] = temp;
// cout<<"temp "<<temp<<" ";
}
//lets check how much water is stored
temp = 0;
for (int i = 0; i < n; i++)
{
temp = temp + (min(left[i], right[i]) - arr[i]);
}
cout<<temp<<endl;
return 0;
}