Peak Performance: Summing Good Array Elements
IntuitionWe need to sum up the elements that are considered "good". An element is "good" if it is strictly greater than its neighbor k positions to the left (if
# Intuition
We need to sum up the elements that are considered "good". An element is "good" if it is strictly greater than its neighbor k positions to the left (if it exists) and strictly greater than its neighbor k positions to the right (if it exists). If one or both of these neighbors don't exist, we simply ignore that condition.

# Approach
1. Initialize a sum variable to 0.
2. Iterate through the array:
    - For each element at index i, check:
         - If the left neighbor at index i - k exists, ensure the current   element is greater than nums[i - k].
        - If the right neighbor at index i + k exists, ensure the current  element is greater than nums[i + k].
    - If both conditions (or the existing ones) are met, consider the  element "good" and add its value to the sum.
3. Return the final sum after processing all elements.

# Complexity
- Time complexity:
Since we traverse the array only once, the algorithm runs in O(n) time, where n is the number of elements in the array.

- Space complexity:
The solution uses only a few extra variables (like the sum accumulator and loop counters), which results in O(1) space complexity.

![Soluations.png](https://assets.leetcode.com/users/images/cf505795-4f6a-4384-8acf-6697a702d99a_1739937306.5759914.png)


# Code
```javascript []
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
const sumOfGoodNumbers = (nums, k) =>
    nums.reduce((total, num, i) => {
        const leftIndex = i - k;
        const rightIndex = i + k;

        //If there is no element on the left or the current element is larger than it
        const leftCondition = leftIndex < 0 || num > nums[leftIndex];

        // If there is no item on the right or the current item is larger than it
        const rightCondition = rightIndex >= nums.length || num > nums[rightIndex];

        // If both conditions are met, we add the value of the element to the sum
        return total + (leftCondition && rightCondition ? num : 0);
    }, 0);

['Array', 'JavaScript']