forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RainWaterHarvesting.java
55 lines (45 loc) · 1.42 KB
/
RainWaterHarvesting.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
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
class RainWaterHarvesting {
public static int saveWater(int[] arr, int size) {
int result = 0;
// traverse the array from second to second last element
for (int i = 1; i < size - 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 < size; j++) {
right = Math.max(right, arr[j]);
}
// Update maximum water value
result += Math.min(left, right) - arr[i];
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter array size:");
int size = scanner.nextInt();
int[] arr = new int[size];
System.out.println("Enter array elements:");
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
System.out.println("Maximum amount of water that can be saved is:" + saveWater(arr, size));
}
}
/*
* Sample input/output:
* Enter array size:
* 10
* Enter array elements:
* 0 2 1 3 0 1 2 1 2 1
* Maximum amount of water that can be saved is:5
*
* Time complexity: O(n^2)
* space complexity: O(1)
*/