-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer With Most Water.cpp
60 lines (48 loc) · 1.21 KB
/
Container With Most Water.cpp
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
56
57
58
59
60
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int maxWater(vector<int> &arr) {
// code here
int left = 0, right = arr.size() - 1;
int max_water = 0;
while (left < right) {
// Calculate the area
int height = min(arr[left], arr[right]);
int width = right - left;
int area = height * width;
// Update maximum area
max_water = max(max_water, area);
// Move the pointer pointing to the smaller height
if (arr[left] < arr[right])
left++;
else
right--;
}
return max_water;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr;
string input;
// Read first array
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
Solution ob;
int res = ob.maxWater(arr);
cout << res << endl << "~" << endl;
}
return 0;
}
// } Driver Code Ends