Beats 100% 🚀 | Simple to Understand
Intuition
We need to find the sum of "good" numbers in the array.
A number is "good" if it is greater than the number k positions before and after it (if they e
# Intuition
1. We need to find the sum of "good" numbers in the array.
2. A number is "good" if it is greater than the number `k` positions before and after it (if they exist).

---


# Approach
1. Iterate through the array.
2. Check if the number is "good" by verifying:
    - If there's a number at index `(i - k)`, it must be smaller than `nums[i]`.
    - If there's a number at index `(i + k)`, it must be smaller than `nums[i]`.
3. If both conditions are met, add `nums[i]` to the sum.
4. Return the final sum.

---


# Complexity
- Time complexity:
O(n) → We iterate through the array once.

- Space complexity:
O(1) → We use only a few extra variables.

---


# Code
```cpp []
class Solution {
public:
    int sumOfGoodNumbers(vector<int>& nums, int k) {
        int n = nums.size();
        int sum = 0;
    
        for (int i = 0; i < n; i++)
        {
            int isGood = 1;
    
            if(i - k >= 0 && nums[i] <= nums[i - k]) isGood = 0;    
            if(i + k < n && nums[i] <= nums[i + k]) isGood = 0;    
            if(isGood) sum += nums[i];
        }
    
        return sum;
    }
};
```

---


['C++']