[Python, Java, C++] Elegant & Short | One Pass | O(n)
Complexity
Time complexity: O(n)
Space complexity: O(1)
Code
# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(1)$$

# Code
```python3 []
class Solution:
    def sumOfGoodNumbers(self, nums: list[int], k: int) -> int:
        n = len(nums)
        good_sum = 0

        for i, num in enumerate(nums):
            if i - k >= 0 and num <= nums[i - k]:
                continue
            if i + k < n and num <= nums[i + k]:
                continue
            good_sum += num

        return good_sum
```
```java []
class Solution {
    public int sumOfGoodNumbers(int[] nums, int k) {
        int n = nums.length;
        int goodSum = 0;

        for (int i = 0; i < n; ++i) {
            if (i - k >= 0 && nums[i] <= nums[i - k])
                continue;
            if (i + k < n && nums[i] <= nums[i + k])
                continue;
            goodSum += nums[i];
        }

        return goodSum;
    }
}
```
```C++ []
class Solution {
public:
    int sumOfGoodNumbers(vector<int>& nums, int k) {
        int n = nums.size();
        int goodSum = 0;

        for (int i = 0; i < n; ++i) {
            if (i - k >= 0 && nums[i] <= nums[i - k])
                continue;
            if (i + k < n && nums[i] <= nums[i + k])
                continue;
            goodSum += nums[i];
        }

        return goodSum;
    }
};
```
['Python', 'C++', 'Java', 'Python3']