-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumWater.java
More file actions
52 lines (39 loc) · 1.28 KB
/
Copy pathmaximumWater.java
File metadata and controls
52 lines (39 loc) · 1.28 KB
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
public class maximumWater {
// Approach 1 with time complexity O(n^2)
public static void largeContainer(int arr[]){
int maxArea = 0;
int n = arr.length;
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
int minHight = Math.min(arr[i], arr[j]);
int width = j - i;
int area = minHight * width;
maxArea = Math.max(maxArea, area);
}
}
System.out.println("The maximum water can be stored is: " + maxArea);
}
// Approach 2 with time complexity O(n)
public static void largeContainer2(int arr[]){
int l = 0;
int r = arr.length - 1;
int maxArea = 0;
while (l < r) {
int minHeight = Math.min(arr[l], arr[r]);
int w = r - l;
int area = minHeight*w;
maxArea = Math.max(maxArea, area);
if(arr[l] < arr[r]){
l++;
}
else{
r--;
}
}
System.out.println("Maximum water can be store as: " + maxArea);
}
public static void main(String[] args) {
int arr[] = {1,8,6,2,5,4,8,3,7};
largeContainer2(arr);
}
}