Skip to content

Commit c9b1ca0

Browse files
authored
Merge pull request #28 from sarthak1810/patch-1
Create TrappingRainwater
2 parents ad35f30 + bf38898 commit c9b1ca0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Java/TrappingRainwater

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
class TrappingRainwater {
3+
static int trap(int[] arr) {
4+
int n = arr.length;
5+
int waterTrapped = 0;
6+
for (int i = 0; i < n; i++) {
7+
int j = i;
8+
int leftMax = 0, rightMax = 0;
9+
while (j >= 0) {
10+
leftMax = Math.max(leftMax, arr[j]);
11+
j--;
12+
}
13+
j = i;
14+
while (j < n) {
15+
rightMax = Math.max(rightMax, arr[j]);
16+
j++;
17+
}
18+
waterTrapped += Math.min(leftMax, rightMax) - arr[i];
19+
}
20+
return waterTrapped;
21+
}
22+
public static void main(String args[]) {
23+
int arr[] = {0,1,0,2,1,0,1,3,2,1,2,1};
24+
System.out.println("The duplicate element is " + trap(arr));
25+
}
26+
}

0 commit comments

Comments
 (0)