-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathContainer with most water.cpp
45 lines (33 loc) · 1.04 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
/*we will use the concept of two pointers. Keep two index, first = 0 and last = n-1 and a value
max_area that stores the maximum area.Run a loop until first is less than the last.Update the
max_area with maximum of max_area and min(array[first] , array[last])*(last-first) if the value at
array[first] is greater the array[last] then update last as last – 1 else update first as first + 1.
Print the maximum area.*/
//Time complexity -O(n);
#include<iostream>
using namespace std;
int maxArea(int A[], int len)
{
int l = 0;
int r = len -1;
int area = 0;
while (l < r)
{
area = max(area, min(A[l],
A[r]) * (r - l));
if (A[l] < A[r])
l += 1;
else
r -= 1;
}
return area;
}
int main()
{
int a[] = {5, 8, 4, 7};
int b[] = {3, 1, 2, 4, 5};
int len1 = sizeof(a) / sizeof(a[0]);
cout << maxArea(a, len1);
int len2 = sizeof(b) / sizeof(b[0]);
cout << endl << maxArea(b, len2);
}