Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Added Trapping Rainwater Question #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Upload Your Code/Arrays/trapping_rainwater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// C++ implementation of the approach

#include <bits/stdc++.h>
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;
}