Next Smaller Element 

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <vector> #include <stack> using namespace std; int main() { int A[ ] = {13 , 7, 6, 12 }; vector<int> res; int size = sizeof(A)/sizeof(A[0]); stack<int> st; st.push(A[size-1]); res.push_back(-1); for(int i=size-2 ; i>=0 ; i--){ while(st.size()>0 &&st.top()>A[i]){ st.pop(); } if(st.size()==0){ res.push_back(-1); }else{ res.push_back(st.top()); } st.push(A[i]); } for(int i=res.size()-1 ; i>=0 ; i--){ cout<<res[i]<<" "; } }
Editor Settings
Theme
Key bindings
Full width
Lines