Greater element

for input
1 2 3 9 4 7
what should be output ?

my code is giving this output which i think is wrong but passing all test cases .
2 3 9 7 -1 -1

my code

#include
#include
using namespace std;
int main() {
int n;
cin>>n;
int a[10000];
for(int i=0;i<n;i++) cin>>a[i];
stack s;
s.push(a[0]);
for(int i=1;i<n;i++){

while(!s.empty()&&s.top()<a[i]){
cout<<a[i]<<" ";
s.pop();
}
s.push(a[i]);
}
while(!s.empty())
{
cout<<-1<<" ";
s.pop();
}

}

The expected output for your input must be:
2 3 4 9 -1 9

For the correct approach, you can refer this:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.