Sum of Good Numbers
IntuitionThe problem requires us to find numbers in the array that are "better" than their neighbors at a specific distance k. The core idea is to compare each
# Intuition
The problem requires us to find numbers in the array that are "better" than their neighbors at a specific distance k. The core idea is to compare each number with elements that are k positions away (both left and right, if they exist).

Understanding "Good" Numbers:

A number is "good" if it is strictly greater than its neighbors at a distance k.
If both neighbors exist, the number should be larger than both.
If only one neighbor exists, the number should be larger than that single neighbor.
Handling Edge Cases:

When i - k is out of bounds (i.e., i < k), we only compare with i + k.
When i + k is out of bounds (i.e., i + k >= n), we only compare with i - k.
When both neighbors exist, we compare with both.
Efficient Iteration:

We iterate through the array once, making only constant-time comparisons for each element.

# Approach
We need to find the sum of all "good" numbers in the given list. A number is considered "good" if it is strictly greater than both the element at index i - k (if it exists) and the element at index i + k (if it exists).

Steps to solve the problem:
1. Initialize a sum variable to keep track of the total sum of good numbers.
2. Iterate through the array while checking each element at index i.
3. Check the validity of the left (i - k) and right (i + k) neighbors:
4. If both exist, the current number must be greater than both to be considered "good."
5. If only one neighbor exists, the current number must be greater than that neighbor.
6. If the number is "good," add it to the sum.
Return the final sum. 

# Complexity
- Time complexity:
O(n) 

- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    int sumOfGoodNumbers(vector<int>& nums, int k) {
        int sum = 0;
        int n = nums.size();
        
        for (int i = 0; i < n; i++) {
            // Check if both indices i-k and i+k are within bounds
            bool left_valid = (i - k >= 0);  // Check if i-k is valid
            bool right_valid = (i + k < n); // Check if i+k is valid
            
            // Check if the current number is greater than both nums[i-k] and nums[i+k] 
            // but only if the indices are valid
            if (left_valid && right_valid) {
                if (nums[i] > nums[i - k] && nums[i] > nums[i + k]) {
                    sum += nums[i];
                }
            } 
            // Handle cases when only one of the indices is valid
            else if (left_valid) {
                if (nums[i] > nums[i - k]) {
                    sum += nums[i];
                }
            } 
            else if (right_valid) {
                if (nums[i] > nums[i + k]) {
                    sum += nums[i];
                }
            }
        }
        
        return sum;
    }
};


```
['Array', 'C++']