 0 ms Beats 100.00% | for beginner only -[CPP / Java / python ] 
🧠 IntuitionWe are given an array of integers nums and an integer k. An element at index i is considered good if:

It is strictly greater than the element at ind
# 🧠 Intuition
We are given an array of integers nums and an integer k. An element at index i is considered good if:

- It is strictly greater than the element at index `i - k` (if it exists), and

- It is strictly greater than the element at index `i + k` (if it exists).

If neither `i - k` nor `i + k` exists, then the element is still considered good.

We are to return the sum of all such "good" elements.
<!-- Describe your first thoughts on how to solve this problem. -->

# 💬 Approach
We iterate through every element in the array. For each element at index i, we check:

- If both `i - k` and `i + k` exist: the element is good only if it's greater than both neighbors.

- If only `i - k` exists: the element is good if it’s greater than that one neighbor.

- If only `i + k` exists: same logic applies.

- If neither index exists: the element is good by default.

We carefully use boundary checks to ensure we do not access out-of-bounds indices.
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:$$O(n)$$ where n is the number of elements in nums
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(1)$$ — we use constant extra space
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# 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++) {
            if ((i - k >= 0 && i + k < n && nums[i] > nums[i - k] && nums[i] > nums[i + k]) ||
                (i - k >= 0 &&i + k >= n &&  nums[i] > nums[i - k]) ||
                (i + k < n && i - k < 0   &&  nums[i] > nums[i + k])   ||
                (i - k < 0 && i + k >= n )
                ) {
                sum += nums[i];
            }
        }
        return sum;
    }
};
```
```java  []
class Solution {
    public int sumOfGoodNumbers(int[] nums, int k) {
        int n = nums.length;
        int sum = 0;

        for (int i = 0; i < n; i++) {
            boolean leftExists = (i - k >= 0);
            boolean rightExists = (i + k < n);

            if ((leftExists && rightExists && nums[i] > nums[i - k] && nums[i] > nums[i + k]) ||
                (leftExists && !rightExists && nums[i] > nums[i - k]) ||
                (!leftExists && rightExists && nums[i] > nums[i + k]) ||
                (!leftExists && !rightExists)) {
                sum += nums[i];
            }
        }

        return sum;
    }
}

```
```py  []
class Solution:
    def sumOfGoodNumbers(self, nums: list[int], k: int) -> int:
        n = len(nums)
        total = 0

        for i in range(n):
            left = i - k
            right = i + k

            if left >= 0 and right < n and nums[i] > nums[left] and nums[i] > nums[right]:
                total += nums[i]
            elif left >= 0 and right >= n and nums[i] > nums[left]:
                total += nums[i]
            elif right < n and left < 0 and nums[i] > nums[right]:
                total += nums[i]
            elif left < 0 and right >= n:
                total += nums[i]

        return total

```
['C++', 'Java', 'Python3']