-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMaximumProductSubarray.cpp
More file actions
60 lines (58 loc) · 1.17 KB
/
MaximumProductSubarray.cpp
File metadata and controls
60 lines (58 loc) · 1.17 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
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
long long pro(int a[], int n, int s, int e)
{
long long p=1, r=1, l=1; int i;
for(i=s; i<=e; i++)
p *= a[i];
for(i=s; i<=e; i++){
l *= a[i];
if(a[i]<0){
break;
}
}
for(i=e; i>=s; i--){
r *= a[i];
if(a[i]<0){
break;
}
}
p /= max(r, l);
return p;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t, n, i, s;
long long cur, tot;
cin >> t;
while(t--){
cin >> n;
int a[n];
for(i=0; i<n; i++)
cin >> a[i];
cur = 1; tot = -11; s = 0;
for(i=0; i<n+1; i++){
if(a[i]==0 || i==n){
if(cur<0){
if(a[s]<0 && a[i-1]<0)
cur /= max(a[s], a[i-1]);
else if(a[s]<0 || a[i-1]<0)
cur /= min(a[s], a[i-1]);
else
cur = pro(a, n, s, i-1);
}
tot = max(tot, cur);
s = -1;
}
cur *= a[i];
if(s==-1){
cur = 1;
s = i+1;
}
}
cout << tot << endl;
}
return 0;
}