#3452
Easy Algorithms Sum of good numbers
Array
69.5% acceptance
Feb 25, 2026
79
28
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn sum_of_good_numbers(nums: Vec<i32>, k: i32) -> i32 {
let k = k as usize;
let n = nums.len();
nums.iter().enumerate().filter(|&(i, &v)| {
(i < k || v > nums[i - k]) && (i + k >= n || v > nums[i + k])
}).map(|(_, &v)| v).sum()
}
}