From 1071fb420d3a6f05cc105bee19e7938815107521 Mon Sep 17 00:00:00 2001 From: Vinod Gupta <89309864+Vinodgupta08@users.noreply.github.com> Date: Sun, 16 Oct 2022 11:22:47 +0530 Subject: [PATCH] Create Rainwater.cpp --- Rainwater.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Rainwater.cpp diff --git a/Rainwater.cpp b/Rainwater.cpp new file mode 100644 index 0000000..429b63d --- /dev/null +++ b/Rainwater.cpp @@ -0,0 +1,43 @@ +// C++ implementation of the approach + +#include +using namespace std; + +// Function to return the maximum +// water that can be stored +int maxWater(int arr[], int n) +{ + // To store the maximum water + // that can be stored + int res = 0; + + // For every element of the array + for (int i = 1; i < n - 1; i++) { + + // Find the maximum element on its left + int left = arr[i]; + for (int j = 0; j < i; j++) + left = max(left, arr[j]); + + // Find the maximum element on its right + int right = arr[i]; + for (int j = i + 1; j < n; j++) + right = max(right, arr[j]); + + // Update the maximum water + res = res + (min(left, right) - arr[i]); + } + + return res; +} + +// Driver code +int main() +{ + int arr[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 }; + int n = sizeof(arr) / sizeof(arr[0]); + + cout << maxWater(arr, n); + + return 0; +}