We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
can be solved easily using stack, similar to maximal rectangle problem.
#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;vector<int>a(n),left(n),right(n);stack<int>Stack;for(auto&i:a)cin>>i;/* left array contains index of previous nearest greatest element right array contains index of next greatest element */for(inti=0;i<n;i++){while(notStack.empty()anda[Stack.top()]<=a[i])Stack.pop();if(notStack.empty())left[i]=Stack.top()+1;Stack.push(i);}/* just to make stack clear */while(notStack.empty())Stack.pop();for(inti=n-1;i>=0;i--){while(notStack.empty()anda[Stack.top()]<=a[i])Stack.pop();if(notStack.empty())right[i]=Stack.top()+1;Stack.push(i);}longlongans=0;for(inti=0;i<n;i++){ans=max(ans,(longlong)left[i]*right[i]);}cout<<ans;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find Maximum Index Product
You are viewing a single comment's thread. Return to all comments →
Things to observe