Sum of Good Numbers
Source: leetcode
Difficulty: Easy
Tags: array

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 indices `i - k` and `i + k` (if those indices exist). If neither of these indices *exists*, `nums[i]` is still considered **good**.


Return the **sum** of all the **good** elements in the array.


 


**Example 1:**



**Input:** nums \= \[1,3,2,1,5,4], k \= 2


**Output:** 12


**Explanation:**


The good numbers are `nums[1] = 3`, `nums[4] = 5`, and `nums[5] = 4` because they are strictly greater than the numbers at indices `i - k` and `i + k`.



**Example 2:**



**Input:** nums \= \[2,1], k \= 1


**Output:** 2


**Explanation:**


The only good number is `nums[0] = 2` because it is strictly greater than `nums[1]`.



 


**Constraints:**


- `2 <= nums.length <= 100`
- `1 <= nums[i] <= 1000`
- `1 <= k <= floor(nums.length / 2)`




C++ solution:
class Solution {
public:
    int sumOfGoodNumbers(vector<int>& nums, int k) {
        int sum = 0;
        int n = nums.size();
        for (int i = 0; i < n; ++i) {
            bool good = true;
            int left = i - k;
            if (left >= 0 && nums[i] <= nums[left]) {
                good = false;
            }
            int right = i + k;
            if (right < n && nums[i] <= nums[right]) {
                good = false;
            }
            if (good) {
                sum += nums[i];
            }
        }
        return sum;
    }
};

Go solution:
func sumOfGoodNumbers(nums []int, k int) int {
    sum := 0
    n := len(nums)
    for i := 0; i < n; i++ {
        good := true
        if left := i - k; left >= 0 && nums[i] <= nums[left] {
            good = false
        }
        if right := i + k; right < n && nums[i] <= nums[right] {
            good = false
        }
        if good {
            sum += nums[i]
        }
    }
    return sum
}

Java solution:
class Solution {
    public int sumOfGoodNumbers(int[] nums, int k) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            boolean good = true;
            int left = i - k;
            if (left >= 0 && nums[i] <= nums[left]) {
                good = false;
            }
            int right = i + k;
            if (right < nums.length && nums[i] <= nums[right]) {
                good = false;
            }
            if (good) {
                sum += nums[i];
            }
        }
        return sum;
    }
}

JavaScript solution:
var sumOfGoodNumbers = function(nums, k) {
    let sum = 0;
    const n = nums.length;
    for (let i = 0; i < n; i++) {
        let isGood = true;
        const left = i - k;
        if (left >= 0 && nums[i] <= nums[left]) isGood = false;
        const right = i + k;
        if (right < n && nums[i] <= nums[right]) isGood = false;
        if (isGood) sum += nums[i];
    }
    return sum;
};

Python solution:
class Solution:
    def sumOfGoodNumbers(self, nums: List[int], k: int) -> int:
        total = 0
        n = len(nums)
        for i in range(n):
            left = i - k
            right = i + k
            left_ok = left < 0 or nums[i] > nums[left]
            right_ok = right >= n or nums[i] > nums[right]
            if left_ok and right_ok:
                total += nums[i]
        return total

Ruby solution:
def sum_of_good_numbers(nums, k)
  sum = 0
  nums.each_with_index do |num, i|
    left = i - k
    right = i + k
    check_left = left >= 0 ? num > nums[left] : true
    check_right = right < nums.size ? num > nums[right] : true
    sum += num if check_left && check_right
  end
  sum
end