-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxsprod.cpp
48 lines (46 loc) · 1.14 KB
/
maxsprod.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
// Question Link - https://www.interviewbit.com/problems/maxspprod/
// Solution
int Solution::maxSpecialProduct(vector<int> &A) {
long long mod = 1e9+7;
stack<long long> lg;
stack<long long> rg;
vector<long long> l;
vector<long long> r;
for(long long i=0; i<A.size(); i++)
{
while(!lg.empty() && A[lg.top()]<=A[i]) lg.pop();
if(lg.empty())
{
l.push_back(-1);
lg.push(i);
}
else
{
l.push_back(lg.top());
lg.push(i);
}
}
for(long long i=A.size()-1; i>=0; i--)
{
while(!rg.empty() && A[rg.top()]<=A[i]) rg.pop();
if(rg.empty())
{
r.push_back(-1);
rg.push(i);
}
else
{
r.push_back(rg.top());
rg.push(i);
}
}
reverse(r.begin(), r.end());
long long maxx = 0;
for(long long i=0; i<A.size(); i++)
{
if(l[i]==-1 || r[i] == -1) continue;
maxx = max(maxx, (l[i]*1LL*r[i]));
}
return maxx%mod;
}
//