From 370b56bdb14e1824ef54952f0fa4064a2f41608e Mon Sep 17 00:00:00 2001 From: ritikaranjan252 <64885616+ritikaranjan252@users.noreply.github.com> Date: Wed, 20 Oct 2021 22:03:47 +0530 Subject: [PATCH] Create Trappingrainwater.py --- C++/Trappingrainwater.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 C++/Trappingrainwater.py diff --git a/C++/Trappingrainwater.py b/C++/Trappingrainwater.py new file mode 100644 index 00000000..7ce567af --- /dev/null +++ b/C++/Trappingrainwater.py @@ -0,0 +1,42 @@ +# Python program to find maximum amount of water that can +# be trapped within given set of bars. + +def findWater(arr, n): + + # left[i] contains height of tallest bar to the + # left of i'th bar including itself + left = [0]*n + + # Right [i] contains height of tallest bar to + # the right of ith bar including itself + right = [0]*n + + # Initialize result + water = 0 + + # Fill left array + left[0] = arr[0] + for i in range( 1, n): + left[i] = max(left[i-1], arr[i]) + + # Fill right array + right[n-1] = arr[n-1] + for i in range(n-2, -1, -1): + right[i] = max(right[i + 1], arr[i]); + + # Calculate the accumulated water element by element + # consider the amount of water on i'th bar, the + # amount of water accumulated on this particular + # bar will be equal to min(left[i], right[i]) - arr[i] . + for i in range(0, n): + water += min(left[i], right[i]) - arr[i] + + return water + + +# Driver program + +arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] +n = len(arr) +print("Maximum water that can be accumulated is", findWater(arr, n)) +