✅✅ Beats 100% 🔥🔥||🧑‍💻 BEGINNER FREINDLY||🌟JAVA||C++||python
Problem Explanation:You are given an array of integers nums and an integer k. An element nums[i] is considered good if it is strictly greater than the elements



# Problem Explanation:
<!-- Describe your first thoughts on how to solve this problem. -->
You are given an array of integers ```nums``` and an integer ```k```. An element ```nums[i]``` is considered good if it is **strictly greater** than the elements at positions ```i - k``` and ```i + k``` (if those positions exist). If neither of these positions exists, the element is still considered **good**. Your task is to return the **sum of all good elements in the array.**
_____________________

# Approach :Brute Force
<!-- Describe your approach to solving the problem. -->
1.Initialize a variable ```totalSum``` to store the sum of "good" elements.

2.Iterate through the array using a loop:

 - For each element at index ```i```, assume it is "good" (```isGood = true```).

  - Check the left neighbor ```(i - k)```:

- If i - k is a valid index and ```nums[i] <= nums[i - k]```, mark the element as not "good" (```isGood = false```).

- Check the right neighbor (```i + k```):

- If ```i + k``` is a valid index and ```nums[i] <= nums[i + k]```, mark the element as not good (```isGood = false```).

- If the element is still marked as "good," add it to ```totalSum```.

3 . Return ```totalSum```after processing all elements.
____

# 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)$$ -->
___

### Visual Representation:
![image.png](https://assets.leetcode.com/users/images/8286dea1-0db9-42ed-a306-bbb427ff4481_1739637185.885747.png)
___



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

class Solution {
public:
    int sumOfGoodNumbers(std::vector<int>& nums, int k) {
        int totalSum = 0; 
        int n = nums.size(); 
        
        for (int i = 0; i < n; i++) {
            bool isGood = true; 
            
            if (i - k >= 0) { 
                if (nums[i] <= nums[i - k]) { 
                    isGood = false; 
                }
            }
            if (i + k < n) { 
                if (nums[i] <= nums[i + k]) { 
                    isGood = false; 
                }
            }
            if (isGood) {
                totalSum += nums[i];
            }
        }
        return totalSum; 
    }
};
```
```python []
class Solution:
    def sumOfGoodNumbers(self, nums, k):
        total_sum = 0
        n = len(nums)
        
        for i in range(n):
            is_good = True
            
            if i - k >= 0:
                if nums[i] <= nums[i - k]:
                    is_good = False
            if i + k < n:
                if nums[i] <= nums[i + k]:
                    is_good = False
            
            if is_good:
                total_sum += nums[i]
        
        return total_sum
```
**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions😊.**

![cat.gif](https://assets.leetcode.com/users/images/9b8c80f6-0f9b-436f-a8e4-99f28e5d4dc1_1739636815.2162175.gif)



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