Beats 100% of C++ Users || Solution using Simple Looping|{\
IntuitionTo efficiently check whether an element is:Greater than all previous elements, andLess than all next elements,You can precompute:The maximum to the lef
# Intuition
To efficiently check whether an element is:

Greater than all previous elements, and

Less than all next elements,

You can precompute:

The maximum to the left of each index (prefix max)

The minimum to the right of each index (suffix min)

With this info:

If nums[i] > premax[i - 1] and nums[i] < sufmin[i + 1] → beauty is 2.

Else if nums[i - 1] < nums[i] < nums[i + 1] → beauty is 1.

Else → beauty is 0.

# Approach
Understand What Makes a Number "Beautiful":
For each element in the array (excluding the first and last), we are asked to evaluate its "beauty" based on certain conditions:

It gets 2 points if it is greater than all elements before it and less than all elements after it.

It gets 1 point if it is simply greater than its immediate left neighbor and less than its immediate right neighbor, but doesn't satisfy the 2-point condition.

It gets 0 points otherwise.

Why Not Check All Elements Manually?
Checking all previous and next elements for every index would be too slow, especially for large arrays. So, we need a more efficient way to find:

The maximum value before each element.

The minimum value after each element.

Precompute Left Maximums (Prefix Maximums):
Create an array where each element stores the maximum value from the start of the array up to that index.
This helps us quickly check if the current element is greater than all previous elements.

Precompute Right Minimums (Suffix Minimums):
Create another array where each element stores the minimum value from that index to the end of the array.
This helps us quickly check if the current element is less than all future elements.

Evaluate Beauty for Each Middle Element:
Now loop through the array from index 1 to n-2, and for each element:

If it is greater than the prefix max (just before it) and less than the suffix min (just after it), add 2 points.

Else if it is just greater than the previous and less than the next element, add 1 point.

Else, add 0.

Return the Total Beauty Score.

✅ Why This Works Well:
This approach is efficient — we only loop through the array a few times.

It avoids nested loops and redundant comparisons.

We reduce the problem of "check all before/after" into quick lookups using precomputed values.

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(1)
![Screenshot 2025-04-04 091332.png](https://assets.leetcode.com/users/images/6472226d-cb4c-42eb-84a4-677500e9e054_1743738394.295046.png)


# Code
```cpp []
class Solution {
public:
    int sumOfBeauties(vector<int>& nums) {
        int n=nums.size();
        vector<int> premax=nums;
        vector<int> sufmin=nums;
        int maxa=nums[0];
        for(int i=0; i<n; i++){
            if(maxa<nums[i]){
                maxa=nums[i];
            }
            else{
                premax[i]=maxa;
            }
        }
        int mini=nums[n-1];
        for(int i=n-1; i>=0; i--){
            if(sufmin[i]<mini){
                mini=sufmin[i];
            }
            else{
                sufmin[i]=mini;
            }
        }
        int ans=0;
        for(int i=1; i<n-1; i++){
            if(nums[i]>premax[i-1] && nums[i]<sufmin[i+1]) ans+=2;
            else if(nums[i]>nums[i-1] && nums[i]<nums[i+1]) ans+=1;
        }
        return ans;
    }
};
```
['Array', 'C++']