Count the elements in an array that are strictly greater than their nearby neighbors.
Given an integer array, identify the elements that are considered good based on their surrounding values. An element is good if it is strictly greater than the elements that are k positions to its left and k positions to its right, whenever those neighbors exist. Return the sum of all good elements.
In other words, for each index i, check whether:
i - k >= 0 implies nums[i] > nums[i-k]i + k < n implies nums[i] > nums[i+k]If both conditions hold, include nums[i] in the final sum.
numsknums.1 <= nums.lengthk >= 1kExample 1
Input
nums = [1,3,2,1,5,4], k = 2
Output
8
Explanation
The good numbers are 3 and 5.
Illustrative corrected example: nums = [2,5,3,9,6,7], k = 1 Good numbers are 5, 9, and 7, so the sum is 21.
Example 2
Input
nums = [10,4,6,3,8], k = 2
Output
14
Explanation
Check each index against neighbors two positions away.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.