i cant pass the last two cases : test case 3 and 4 .
here is my code :
#include <bits/stdc++.h>
using namespace std;
// Function to print Next Greater Element for each element of the array
void nextGreater(int arr[], long long int n)
{
stack s;
s.push(arr[0]);
for(int i=1;i<n;i++){
int current_element = arr[i];
if(s.empty()){
s.push(arr[i]);
continue;
}
if(!s.empty() and s.top() >= arr[i]){
cout<<s.top()<<","<<-1<<endl;
s.pop();
}
while(!s.empty() and s.top()<arr[i]){
cout<<s.top()<<","<<arr[i]<<endl;
s.pop();
}
s.push(arr[i]);
}
while(!s.empty()){
cout<<s.top()<<","<<-1<<endl;
s.pop();
}
}
// The Main Function
int main()
{
long long int t;
cin>>t;
while(t–){
long long int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
nextGreater(arr, n);
}
return 0;
}