O(n) 🔥 | | C++
Complexity

Time complexity:
O(n)


Space complexity:
O(1)

Code
# Complexity
- Time complexity:
$$O(n)$$ 

- Space complexity:
$$O(1)$$

# Code
```cpp []
class Solution {
public:
    int sumOfGoodNumbers(vector<int>& nums, int k) {
        int n = nums.size(), sum = 0;

        for (int i = 0; i < n; i++) {
            if ((i - k) < 0 && (i + k) > (n - 1)) {
                sum += nums[i];
            } else if ((i - k) < 0) {
                if (nums[i + k] < nums[i]) {
                    sum += nums[i];
                }
            } else if ((i + k) > (n - 1)) {
                if (nums[i - k] < nums[i]) {
                    sum += nums[i];
                }
            } else if ((nums[i] > nums[i + k]) && (nums[i] > nums[i - k])) {
                sum += nums[i];
            }
        }
        return sum;
    }
};
```
['Array', 'C++']