using prefix and suffix array approach
\n# Code\n\nclass Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n=nums.size();\n        vector<int> prefix(n+1),suffix(n+1);\n   
\n# Code\n```\nclass Solution {\npublic:\n    int sumOfBeauties(vector<int>& nums) {\n        int n=nums.size();\n        vector<int> prefix(n+1),suffix(n+1);\n        prefix[0]=nums[0];\n        suffix[n-1]=nums[n-1];\n        int sum=0;\n        for(int i=1;i<nums.size()-1;i++)\n        {\n            prefix[i]=max(prefix[i-1],nums[i]);\n        }\n         for(int i=n-2;i>=0;i--)\n        {\n            suffix[i]=min(suffix[i+1],nums[i]);\n        }\n        for(int i=1;i<nums.size()-1;i++)\n        {\n            if(nums[i]>prefix[i-1]&&nums[i]<suffix[i+1])\n            sum+=2;\n            else if (nums[i-1]<nums[i]&&nums[i]<nums[i+1])\n            sum+=1;\n        }\n        return sum;\n        \n    }\n};\n```
['C++']