JuniorArray

Sum of Good Numbers

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
Inputnums = [1,3,2,1,5,4], k = 2
Output12
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
Inputnums = [2,1], k = 1
Output2
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)

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate