Skip to main content
Back to problems
#985
Medium Algorithms

Sum of even numbers after queries

Array Simulation
68.8% acceptance
Feb 25, 2026
2136
323
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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn sum_even_after_queries(mut nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
    let mut even_sum: i32 = nums.iter().filter(|&&x| x % 2 == 0).sum();
    let mut res = Vec::new();
    for q in queries {
      let (val, idx) = (q[0], q[1] as usize);
      if nums[idx] % 2 == 0 { even_sum -= nums[idx]; }
      nums[idx] += val;
      if nums[idx] % 2 == 0 { even_sum += nums[idx]; }
      res.push(even_sum);
    }
    res
  }
}