Find the Greatest Element

This is my code:

include

include
using namespace std;
int main() {
int n;
cin>>n;
int arr[1000000];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++){
int temp=arr[i];
rotate(arr,arr+i,arr+n);
int ub=upper_bound(arr,arr+n,temp)-arr;

	if(ub==n)
	cout<<-1<<" ";
	else if(ub<n&&ub>=1)
	cout<<arr[ub]<<" ";
	
    rotate(arr,arr+n-i,arr+n);

}
return 0;

}

I dont know why it failed 1 testcase

Is there any TA connected??

your approach is incorrect.
upper_bound() works only for sorted arrays. so you can not use upper_bound()

naive solution: you can do it in O(n^2). Find next greater element for every element through linear search in O(n).
Hint for better solution: it is application of stack. think about it.

thanks

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.