Just check this out. Samajh aa jaaye shayad


#1

#define ll int
vectorNGR(vector&A)
{
ll sizz=A.size();
vectorans(sizz,0);
stacks;
for(int i=sizz-1;i>=0;i–)
{
while(!s.empty() && A[s.top()]<=A[i])
s.pop();
if(s.empty())
ans[i]=sizz;

else
  ans[i]=s.top();
s.push(i);

}
return ans;
}

int Solution::solve(vector &A)
{
int n=A.size();
vectorngr=NGR(A);
vectorsr(n);
int small=INT_MAX;
for(int i=n-1;i>=0;i–)
{
sr[i]=small;
small=min(small,A[i]);
}
//cout<<“dnjasnfjsvjdn”<<endl;
for(int i=0;i<n;i++)
{

    if(ngr[i]!=n && sr[ngr[i]]<=A[i])return 0;
}

return 1;   

}