Finding the Sum of Good Numbers in an Array Based on k-Distance Neighbors
IntuitionThe problem requires identifying elements in an array that are strictly greater than their neighbors at positions i - k and i + k (if those indices exi
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires identifying elements in an array that are strictly greater than their neighbors at positions i - k and i + k (if those indices exist).

* If an element is greater than these neighbors (or if they don’t exist), it is considered a "good" element.
* The goal is to find the sum of all such good elements.

To avoid accessing invalid indices, we need to handle boundary conditions properly.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Iterate through the array using a loop.
2. Check if the element at index i is "good":
3. * If i - k is within bounds, check if nums[i] is greater than nums[i - k].
   * If i + k is within bounds, check if nums[i] is greater than nums[i + k].
   * If both conditions hold (or if one of them is out of bounds), mark the element as good.
4. Sum up all the good elements and return the result.

This ensures that we efficiently determine which elements qualify as "good" while avoiding any out-of-bounds errors.
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```java []
class Solution {
    public int sumOfGoodNumbers(int[] nums, int k) {
     int sum=0;
     for(int i=0;i<nums.length;i++){
     boolean isGood = true;
     
     if((i-k) >= 0 && nums[i] <= nums[i-k]){
        isGood = false;
     }   

     if((i+k) < nums.length && nums[i] <= nums[i+k]){
        isGood = false;
     }

     if(isGood){
        sum+=nums[i];
     }
    }

     return sum;
    }
}
```
['Java']