-
Notifications
You must be signed in to change notification settings - Fork 0
/
Next_Greater_Element.cpp
44 lines (39 loc) · 1.02 KB
/
Next_Greater_Element.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;
vector<int> nextgreatorelement(vector<int>& v)
{
int n= v.size();
vector<int> nge(n,-1);
stack<int> st;
for(int i=2*n-1; i>=0; i--)
{
// check whether stack is empty or not if not then check element of stack is lesser then our array ith element if yes then pop them all lesser element
while(!st.empty() && st.top() <= v[i%n])
{
st.pop();
}
if(i<n)
{
if(!st.empty()) //if stack not empty push top elemt or nearest greater integer
{
nge[i] = st.top();
}
else //if stack is empty push -1
{
nge[i] = -1;
}
}
st.push(v[i%n]); //push ith elemt into stack
}
return nge;
}
int main()
{
vector<int> v = {2,10,12,1,11};
vector<int> result = nextgreatorelement(v);
for(int i=0; i<result.size(); i++)
{
cout<<result[i]<<" ";
}
cout<<endl;
}