Python solution with O(n) Complexity
IntuitionWe are given:An array nums of integers.
An integer k.An element nums[i] is considered good if:
✅ nums[i] is strictly greater than nums[i-k] (left neigh
# Intuition
We are given:

An array nums of integers.
An integer k.

An element nums[i] is considered good if:
✅ nums[i] is strictly greater than nums[i-k] (left neighbor) and nums[i+k] (right neighbor), if those indices exist.
✅ If either i-k or i+k is out of bounds, we ignore that side for comparison.
✅ If both indices are out of bounds, nums[i] is automatically good
# Approach
We need to check each element nums[i] and determine whether it satisfies the "good" condition.

**Steps to Solve**:
- Define a helper function safe_get(nums, index)
- This function safely gets nums[index], returning None if index is out of range.
- Iterate through the array
- Get nums[i-k] (left neighbor) and nums[i+k] (right neighbor) safely.
- Check if nums[i] is greater than its existing neighbors.
- If it satisfies the condition, add it to the good sum.
- Return the final sum of all good elements.
# Complexity
- Time complexity:
O(n) → We iterate through the list once (O(n)) and perform O(1) operations for each element.

- Space complexity:
O(1) → We only use a few extra variables (good, left, right), so space usage is constant.

# Code
```python3 []
class Solution:
    def sumOfGoodNumbers(self, nums: List[int], k: int) -> int:
        good=0
# Function to safely get nums[index], returning None if out of bounds
        def safe_get(nums, index):
            return nums[index] if 0 <= index < len(nums) else None
        for i in range(len(nums)):
            left=safe_get(nums,i-k) # Left neighbour
            right=safe_get(nums,i+k) # Right neighbour
            if (left is None or nums[i] > left) and (right is None or nums[i] > right):
                good+=nums[i]   
        return good          

        
```

**Please Upvote if you find it helpful**
[]()![6hml63n56uuc1.gif](https://assets.leetcode.com/users/images/c6245692-db81-4bb6-977b-21d37fd849a1_1739706962.2043722.gif)

['Python3']