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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums and an array queries where queries[i] = [vali, indexi].
For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.
Return an integer array answer where answer[i] is the answer to the ith query.
Example 1:
Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]] Output: [8,6,2,4] Explanation: At the beginning, the array is [1,2,3,4]. After adding 1 to nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8. After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6. After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2. After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
Example 2:
Input: nums = [1], queries = [[4,0]] Output: [0]
Constraints:
1 <= nums.length <= 104-104 <= nums[i] <= 1041 <= queries.length <= 104-104 <= vali <= 1040 <= indexi < nums.lengthProblem summary: You are given an integer array nums and an array queries where queries[i] = [vali, indexi]. For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums. Return an integer array answer where answer[i] is the answer to the ith query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,3,4] [[1,0],[-3,1],[-4,0],[2,3]]
[1] [[4,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #985: Sum of Even Numbers After Queries
class Solution {
public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
int s = 0;
for (int x : nums) {
if (x % 2 == 0) {
s += x;
}
}
int m = queries.length;
int[] ans = new int[m];
int k = 0;
for (var q : queries) {
int v = q[0], i = q[1];
if (nums[i] % 2 == 0) {
s -= nums[i];
}
nums[i] += v;
if (nums[i] % 2 == 0) {
s += nums[i];
}
ans[k++] = s;
}
return ans;
}
}
// Accepted solution for LeetCode #985: Sum of Even Numbers After Queries
func sumEvenAfterQueries(nums []int, queries [][]int) (ans []int) {
s := 0
for _, x := range nums {
if x%2 == 0 {
s += x
}
}
for _, q := range queries {
v, i := q[0], q[1]
if nums[i]%2 == 0 {
s -= nums[i]
}
nums[i] += v
if nums[i]%2 == 0 {
s += nums[i]
}
ans = append(ans, s)
}
return
}
# Accepted solution for LeetCode #985: Sum of Even Numbers After Queries
class Solution:
def sumEvenAfterQueries(
self, nums: List[int], queries: List[List[int]]
) -> List[int]:
s = sum(x for x in nums if x % 2 == 0)
ans = []
for v, i in queries:
if nums[i] % 2 == 0:
s -= nums[i]
nums[i] += v
if nums[i] % 2 == 0:
s += nums[i]
ans.append(s)
return ans
// Accepted solution for LeetCode #985: Sum of Even Numbers After Queries
impl Solution {
pub fn sum_even_after_queries(mut nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let mut s: i32 = nums.iter().filter(|&x| x % 2 == 0).sum();
let mut ans = Vec::with_capacity(queries.len());
for query in queries {
let (v, i) = (query[0], query[1] as usize);
if nums[i] % 2 == 0 {
s -= nums[i];
}
nums[i] += v;
if nums[i] % 2 == 0 {
s += nums[i];
}
ans.push(s);
}
ans
}
}
// Accepted solution for LeetCode #985: Sum of Even Numbers After Queries
function sumEvenAfterQueries(nums: number[], queries: number[][]): number[] {
let s = nums.reduce((acc, x) => acc + (x % 2 === 0 ? x : 0), 0);
const ans: number[] = [];
for (const [v, i] of queries) {
if (nums[i] % 2 === 0) {
s -= nums[i];
}
nums[i] += v;
if (nums[i] % 2 === 0) {
s += nums[i];
}
ans.push(s);
}
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.