Find the greater element - stack

Doesnt pass first test case. :frowning:

#include
#include
using namespace std;
#define ll long long int

int main() {
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++){
cin>>a[i];
}
stack s;
s.push(a[0]);
for(ll i=1;i<n;i++){
while(!s.empty() and s.top()<a[i]){
cout<<a[i]<<" “;
s.pop();
}
s.push(a[i]);
}
while(!s.empty()){
cout<<-1<<” ";
s.pop();
}
return 0;
}

Hello @maazsaad please wait i am checking your code:

Hello @maazsaad your code is failing because you have to check in the circular way it is written that you are given with the circular array.
i think you have to do this question with indexing instead of pushing the elements in it.
because with indexing you can access the elements and append the next greater at that index.
do you want me to change your code according to that?

yes, kindly help me out.

@maazsaad check this code:
https://ide.codingblocks.com/s/428583
in this i have also checked for if the first element is greater then the last element then the next greater for the last element will be first element.

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.

1 Like