-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_with_max_water_optimized.cpp
More file actions
52 lines (38 loc) · 1.01 KB
/
container_with_max_water_optimized.cpp
File metadata and controls
52 lines (38 loc) · 1.01 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
#include<bits/stdc++.h>
using namespace std;
/* optimized with only one for loop by using two pointer approch
first pointer is pointing to 1st element, 2nd pointer to last element
we will check for bigger element between(a,b),
if b is bigger - we will do a++ or else
a is bigger - we will do b--
so basically finding two elements with minimum gap AND
largest distance so starting from first and last end of the array
*/
int max_water(vector<int> & vec)
{
int area, maxArea =0;
for(int i=0, j= vec.size() -1 ;i< vec.size() -1 && j+1 ;)
{
area = min(vec[i],vec[j]) * (j -i);
maxArea = (maxArea < area) ? area : maxArea ;
vec[i] < vec[j] ? ++i : --j;
}
return maxArea;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("inputf.in","r",stdin);
freopen("outputf.out","w",stdout);
#endif
int vecSize, temp;
cin>>vecSize;
vector<int> myVec;
for(int i=0; i< vecSize; ++i)
{
cin>>temp;
myVec.push_back(temp);
}
cout<<max_water(myVec);
return 0;
}