C++ || Prefix and Suffix || Array
\nclass Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n=nums.size();\n        vector<int>left(n,-1); //store next greater element
```\nclass Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n=nums.size();\n        vector<int>left(n,-1); //store next greater element in left side\n        vector<int>right(n,-1); //store next smaller element in right side\n        stack<int>st;\n        for(int i=n-1;i>=0;i--){\n            while(!st.empty() and nums[st.top()]<=nums[i]){\n                left[st.top()]=nums[i];\n                st.pop();\n            }\n            st.push(i);\n        }\n        while(!st.empty()){\n            st.pop();\n        }\n        for(int i=0;i<n;i++){\n            while(!st.empty() and nums[st.top()]>=nums[i]){\n                right[st.top()]=nums[i];\n                st.pop();\n            }\n            st.push(i);\n        }\n        int ans=0;\n        for(int i=1;i<n-1;i++){\n            if(left[i]==-1 and right[i]==-1){\n                ans+=2;\n            }else if(nums[i-1]<nums[i] and nums[i]<nums[i+1]){\n                ans+=1;\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']