Find the greater element doubt

Link to the question - https://online.codingblocks.com/player/24941/content/10040/4976
First of all, my answer isn’t complete, I know that. I haven’t yet considered a circular array, but just a simple array, and tried out to find the answer using stacks.

I tried putting the answers into a vector, but it didn’t show any output. https://ide.codingblocks.com/s/83921
Then, everything still being the same, I tried putting the answer into an array, and the output is coming as expected. https://ide.codingblocks.com/s/83922

Can anyone please tell me what’s going wrong with my vector code?

@pmahajan hey pulkit
update you code with this function
void printnge(int *a,int n){
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();
}

}

Thanks a lot for your help, my code worked fine though (for its intended purpose :stuck_out_tongue: )
Figured out my problem.
Apparently, changing my while condition from
s.top()<arr[i] && !s.empty()
to
!s.empty() && s.top()<arr[i]
did the trick.
Any Idea why’d that happen?

Thanks for this though!

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.