Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions nextgreaterelement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<map>
#define ll long long
using namespace std;


void nextLargerElement(vector<long long> arr, int n){

stack<ll>s;
vector<ll>vect1;
vector<ll>vect(n,-1);
for(int i=0;i<n;i++){
while(!s.empty() && arr[s.top()]<arr[i]){
vect[s.top()]=(i);
s.pop();
}s.push(i);
}
for(int i=0;i<n;i++){
if(vect[i]==-1)
vect1.push_back(-1);
else
vect1.push_back(arr[vect[i]]);

}
for(int i=0;i<vect1.size();i++)
cout<<vect1[i]<<" ";
}


int main()
{
int t;
cin>>t;
while(t--)
{

int n;
cin>>n;
vector<long long> arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];


nextLargerElement(arr, n);

}
return 0;
}