forked from itsjohnty/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainWaterTrapping.java
46 lines (36 loc) · 986 Bytes
/
rainWaterTrapping.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Java code to implement of the approach
class GFG {
// Function to return the maximum
// water that can be stored
public static int maxWater(int[] arr, int n)
{
// To store the maximum water
// that can be stored
int res = 0;
// For every element of the array
// except first and last element
for (int i = 1; i < n - 1; i++) {
// Find maximum element on its left
int left = arr[i];
for (int j = 0; j < i; j++) {
left = Math.max(left, arr[j]);
}
// Find maximum element on its right
int right = arr[i];
for (int j = i + 1; j < n; j++) {
right = Math.max(right, arr[j]);
}
// Update maximum water value
res += Math.min(left, right) - arr[i];
}
return res;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
int n = arr.length;
System.out.print(maxWater(arr, n));
}
}
// This code is contributed by Debidutta Rath