Quick actions

cmd+k|ctrl+k

Navigation

Languages

Next Smaller Element 

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-05-25T10:45:58.598821Z

Updated

2021-05-25T10:45:58.598821Z

#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]<<" ";
    }
    
    
}
INFO