first test case not passing!
#include<bits/stdc++.h>
using namespace std;
void solve(int a[], int n){
vector v;
stack s;
for(int i=n-1;i>=0;i–){
if(s.size()==0){
v.push_back(-1);
}
if(s.size()>0 && a[i]<s.top()){
v.push_back(s.top());
}
if(s.size()>0 && s.top()<=a[i]){
while(s.size()>0 && s.top()<=a[i]){
s.pop();
}
if(s.size()==0)
v.push_back(-1);
else{
v.push_back(s.top());
}
}
s.push(a[i]);
}
reverse(v.begin(),v.end());
for(auto x:v){
cout<<x<<" ";
}
}
int main() {
int n; cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
solve(a,n);
}