-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #658 from Omgupta0312/new_branch1
Rainwatertrap Problem
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
Program's_Contributed_By_Contributors/C++ Programs/rainwatertrap.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int rwt(int a[], int n) | ||
{ | ||
int res = 0; | ||
int lmax[n], rmax[n]; | ||
|
||
lmax[0] = a[0]; | ||
for (int i = 1; i < n - 1; i++) | ||
lmax[i] = max(a[i], lmax[i - 1]); | ||
|
||
rmax[n - 1] = a[n - 1]; | ||
for (int i = n - 2; i >= 0; i--) | ||
rmax[i] = max(a[i], rmax[i + 1]); | ||
|
||
for (int i = 1; i < n - 1; i++) | ||
res = res + min(lmax[i], rmax[i]) - a[i]; | ||
|
||
return res; | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[] = {5, 4, 6, 0, 4, 5}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
cout << rwt(arr, n) << endl; | ||
|
||
return 0; | ||
} |