😎😎 || Simplest Java / C++ / C Solution || ✅✅ || 💀💀💀
Intuition
A number is considered "good" if it is strictly greater than its k-distance neighbors (if they exist). By iterating through the array and checking the
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- A number is considered "good" if it is strictly greater than its `k`-distance neighbors (if they exist). By iterating through the array and checking these conditions, we can efficiently identify and sum up all such elements.
---
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Iterate Through the Array:**
- Traverse each element in `nums` to check if it satisfies the good number condition.
2. **Check the Validity of Index **`i - k` and `i + k`**:**
- If `i - k` is out of bounds, check only `nums[i] > nums[i + k]`.
- If `i + k` is out of bounds, check only `nums[i] > nums[i - k]`.
- If both indices exist, check if `nums[i]` is strictly greater than both 
`nums[i - k]` and `nums[i + k]`.
3. **Add the Good Elements to the Sum:**
- If `nums[i]` satisfies the condition, add it to the `sum`.
4. **Return the Total Sum**.
---
# 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++) {
            
            if (i - k < 0) {
                if (nums[i] > nums[i + k])
                    sum = sum + nums[i];
            } 

            else if (i + k >= nums.length) {
                if (nums[i] > nums[i - k])
                    sum = sum + nums[i];
            } 

            else {
                if (nums[i] > nums[i - k] && nums[i] > nums[i + k])
                    sum = sum + nums[i];
            }
        }

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

        for (int i = 0; i < nums.size(); i++) {
            
            if (i - k < 0) {
                if (nums[i] > nums[i + k])
                    sum = sum + nums[i];
            } 

            else if (i + k >= nums.size()) {
                if (nums[i] > nums[i - k])
                    sum = sum + nums[i];
            } 

            else {
                if (nums[i] > nums[i - k] && nums[i] > nums[i + k])
                    sum = sum + nums[i];
            }
        }

        return sum;
    }
};
```
```C []
int sumOfGoodNumbers(int* nums, int numsSize, int k) {
    int sum = 0;

        for (int i = 0; i < numsSize; i++) {
            
            if (i - k < 0) {
                if (nums[i] > nums[i + k])
                    sum = sum + nums[i];
            } 

            else if (i + k >= numsSize) {
                if (nums[i] > nums[i - k])
                    sum = sum + nums[i];
            } 

            else {
                if (nums[i] > nums[i - k] && nums[i] > nums[i + k])
                    sum = sum + nums[i];
            }
        }

        return sum;
}
```
---
![sution section image.jpeg](https://assets.leetcode.com/users/images/ea7275e5-ee1b-4cbf-aef7-21cf7466ca65_1742029001.8598244.jpeg)

['Array', 'C', 'C++', 'Java']