# 985. Sum of Even Numbers After Queries

## Description

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] <= 104
• 1 <= queries.length <= 104
• -104 <= vali <= 104
• 0 <= indexi < nums.length

## Solutions

• 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;
}
}

• class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& nums, vector<vector<int>>& queries) {
int s = 0;
for (int x : nums) {
if (x % 2 == 0) {
s += x;
}
}
vector<int> ans;
for (auto& 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.push_back(s);
}
return ans;
}
};

• 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


• 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
}

• function sumEvenAfterQueries(nums: number[], queries: number[][]): number[] {
let s = 0;
for (const x of nums) {
if (x % 2 === 0) {
s += x;
}
}
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;
}


• /**
* @param {number[]} nums
* @param {number[][]} queries
* @return {number[]}
*/
var sumEvenAfterQueries = function (nums, queries) {
let s = 0;
for (const x of nums) {
if (x % 2 === 0) {
s += x;
}
}
const ans = [];
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;
};