Error in test case 1 but output is same as mentioned

#include
#include
using namespace std;
int main() {

int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
	cin>>arr[i];
}
int ans[n]={0};
stack<int >s;
for(int i=0;i<n;i++){
	while(!s.empty() && arr[s.top()]<arr[i]){
		ans[s.top()]=arr[i];
		s.pop();
	}
	s.push(i);
}
while(!s.empty()){
	ans[s.top()]=-1;
	s.pop();
}
for(int i=0;i<n;i++){
	cout<<ans[i]<<" ";
}
cout<<endl;
return 0;

}

hi @kumawatnitesh093,
, start from index i + 1 and go uptil the last index after which we start looking for the greater number from the starting index of the array since array is circular.

take care of this
9 1 2 ans is -1 2 9 (for 2 ans is 9 as array is circular)

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.