Find the greater next element

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);
}

hello @daspradhi1812

this solution will work only for linear array , we are given circular array thats why it is failing for some cases.

1 Like

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.