Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 1001 <= nums[i] <= 10001 <= k <= floor(nums.length / 2)Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,3,2,1,5,4] 2
[2,1] 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3452: Sum of Good Numbers
class Solution {
public int sumOfGoodNumbers(int[] nums, int k) {
int ans = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
if (i >= k && nums[i] <= nums[i - k]) {
continue;
}
if (i + k < n && nums[i] <= nums[i + k]) {
continue;
}
ans += nums[i];
}
return ans;
}
}
// Accepted solution for LeetCode #3452: Sum of Good Numbers
func sumOfGoodNumbers(nums []int, k int) (ans int) {
for i, x := range nums {
if i >= k && x <= nums[i-k] {
continue
}
if i+k < len(nums) && x <= nums[i+k] {
continue
}
ans += x
}
return
}
# Accepted solution for LeetCode #3452: Sum of Good Numbers
class Solution:
def sumOfGoodNumbers(self, nums: List[int], k: int) -> int:
ans = 0
for i, x in enumerate(nums):
if i >= k and x <= nums[i - k]:
continue
if i + k < len(nums) and x <= nums[i + k]:
continue
ans += x
return ans
// Accepted solution for LeetCode #3452: Sum of Good Numbers
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3452: Sum of Good Numbers
// class Solution {
// public int sumOfGoodNumbers(int[] nums, int k) {
// int ans = 0;
// int n = nums.length;
// for (int i = 0; i < n; ++i) {
// if (i >= k && nums[i] <= nums[i - k]) {
// continue;
// }
// if (i + k < n && nums[i] <= nums[i + k]) {
// continue;
// }
// ans += nums[i];
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3452: Sum of Good Numbers
function sumOfGoodNumbers(nums: number[], k: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
if (i >= k && nums[i] <= nums[i - k]) {
continue;
}
if (i + k < n && nums[i] <= nums[i + k]) {
continue;
}
ans += nums[i];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.