C++ O(N) || Stack
Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\nApproach: Nearest Smaller on Right\n\n\nclass Solution {\npublic:\n    \n    vector<
**Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n**Approach:** Nearest Smaller on Right\n\n```\nclass Solution {\npublic:\n    \n    vector<int> smaller(vector<int>& a){\n        int n=a.size();\n        vector<int> nsr(n,n);\n        stack<pair<int,int>> st;\n        for(int i=n-1;i>=0;i--){\n            while(st.size()>0 && st.top().first>a[i]) st.pop();\n            if(st.size()!=0){\n                nsr[i]=st.top().second;\n            }\n            st.push({a[i],i});\n        }\n        return nsr;\n    }\n    \n    vector<int> greater(vector<int>& a){\n        int n=a.size();\n        vector<int> ngl(n,-1);\n        stack<pair<int,int>> st;\n        for(int i=0;i<n;i++){\n            while(st.size()>0 && st.top().first<a[i]) st.pop();\n            if(st.size()!=0){\n                ngl[i]=st.top().second;\n            }\n            st.push({a[i],i});\n        }\n        return ngl;\n    }\n    \n    int sumOfBeauties(vector<int>& a) {\n        int n=a.size();\n        \n        vector<int> nsr=smaller(a);  // storing index of Nearest Smaller in Right to current element\n        vector<int> ngl=greater(a); // storing index of Nearest Greater in Left to current element\n        \n        vector<bool> type1(n,false);\n        int ans=0;\n        for(int i=1;i<n-1;i++){\n            if(ngl[i]==-1 && nsr[i]==n){\n                ans+=2;\n                type1[i]=true;\n            }\n        }\n        for(int i=1;i<n-1;i++){\n            if(type1[i]==false && a[i-1]<a[i] && a[i]<a[i+1]) ans++;\n        }\n        return ans;\n    }\n};\n```
['Stack', 'C++']