Simple C++  Solution | O(N) Time Complexity | O(N) Space Complexity
Problem\nGiven an array nums[] and for each index i (1 <= i <= nums.length - 2) the beauty of nums[i] equals:\n\n2, if nums[j] < nums[i] < nums[k], for all 0 <=
**Problem**\nGiven an array nums[] and for each index i (1 <= i <= nums.length - 2) the beauty of nums[i] equals:\n\n2, if nums[j] < nums[i] < nums[k], for all 0 <= j < i and for all i < k <= nums.length - 1.\n1, if nums[i - 1] < nums[i] < nums[i + 1], and the previous condition is not satisfied.\n0, if none of the previous conditions holds.\n\n**Approach**\nconsider the initial value of **sum=0**\n**Step 1 :**\n*For each index i (i>=1 and i<=n-2)  check if i is the greatest element in range [0,i] and i is the smallest element in range  [i,n] where n is the size of the array.\nif **true** increment the value of **sum** by 2 i.e **sum=sum+2***\n\n**Step 2**\nIf **Step 1** fails then check whether **nums[i]>nums[i-1] and  nums[i]<nums[i+1]**  \nif **true** increment the value of **sum** by 1 i.e. **sum++**\n\nreturn the value **sum** after iterating the array\n\n**Code**\n```\nint sumOfBeauties(vector<int>& nums) {\n\n        int size = nums.size();\n        \n        int maxval = nums[0];\n        \n        // intial sum value is 0 \n        int sum = 0;\n        // array to store min value upto particular index from end\n        int minvalues[size]; \n        \n        int minval = nums[size-1];\n        minvalues[size-1] = nums[size-1];\n        \n        //storing the minimum value upto a particular index \'i\' from the end\n        for(int i=size-2;i>=1;i--) {\n            minval = min(minval,nums[i]);\n            minvalues[i] = minval;\n        }\n        \n        for(int i=1;i<size-1;i++) {\n           \n            //checking the first condition\n            if(nums[i]>maxval && nums[i]<minvalues[i+1])\n            {\n                sum+=2;\n            }\n            \n            //checking the second condition if first condition fails\n            else if((nums[i]>nums[i-1]) && (nums[i]<nums[i+1]))\n            {\n                sum++;\n            }\n            \n            //updating max value as we are not using any additional space for storing maxvalues\n            maxval = max(maxval,nums[i]);\n        }\n        \n        return sum;      \n    }\n```\nIn this solution we are prefetching min values  because it is gives us the minimum value in the range **[i,n]** in O(1) time if we don\'t do it we need to calculate the minimum value in the range **[i,n]** each and every time.Where as max value before an index **i** can easily be obtained during the traversal.\n\nDo **upvote** the post if you find it useful.Feel free to **comment** if you have any queries.\n\n**Happy Coding** \uD83D\uDE42
['C']