Skip to content
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
40 changes: 40 additions & 0 deletions Code/trapping rainwater problem in O(1) space
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def findWater(arr,n):

result = 0


left_max = 0
right_max = 0

lo = 0
hi = n-1

while(lo <= hi):

if(arr[lo] < arr[hi]):

if(arr[lo] > left_max):

left_max = arr[lo]
else:

result += left_max - arr[lo]
lo+=1

else:

if(arr[hi] > right_max):

right_max = arr[hi]
else:
result += right_max - arr[hi]
hi-=1

return result


arr = [int(x) for x in input().split()]
n = len(arr)

print("Maximum water that can be accumulated is ",
findWater(arr, n))