-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer With Most Water.java
More file actions
50 lines (40 loc) · 1.18 KB
/
Copy pathContainer With Most Water.java
File metadata and controls
50 lines (40 loc) · 1.18 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
// Date : 27.12.2022
// problem statement: Container With Most Water( Mid Level )
// Time complexity : O(N)
// Space complexity : O(1)
class Solve{
long maxArea(int A[], int len){
// return at the end
int ans = 0;
// creating three variable left right and the area's width
int left,right,width;
//given height constrant is 100
for(int height=1;height<=100;height++)
{
//start from the left side with increment
left =0;
for(int i=0;i<len ;i++)
{
if(A[i] >= height)
{
left=i;
break;
}
}
//start from the end(right) side with decrement
right =0;
for(int i=len-1;i>=0;i--)
{
if(A[i] >= height)
{
right = i;
break;
}
}
width = (right-left);
// we have to return the maximum area
ans = Math.max(ans,height*width);
}
return ans;
}
}