> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sum of Even Numbers After Queries

> Tested Python solution for LeetCode 985 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 985, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/sum-even-after-queries/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 985   # by problem number
lcpy gen -s sum_even_after_queries   # by problem name
```

## Problem

You are given an integer array `nums` and an array `queries` where `queries[i] = [val_i, index_i]`.

For each query `i`, first, apply `nums[index_i] = nums[index_i] + val_i`, then print the sum of the even values of `nums`.

Return an integer array `answer` where `answer[i]` is the answer to the `i-th` query.

### Examples

```
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.
```

```
Input: nums = [1], queries = [[4,0]]
Output: [0]
```

### Constraints

* 1 \<= nums.length \<= 10^4
* -10^4 \<= nums\[i] \<= 10^4
* 1 \<= queries.length \<= 10^4
* -10^4 \<= val\_i \<= 10^4
* 0 \<= index\_i \< nums.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sum_even_after_queries/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sum_even_after_queries/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + q)
    # Space: O(q)
    def sum_even_after_queries(self, nums: list[int], queries: list[list[int]]) -> list[int]:
        even_sum = sum(num for num in nums if num % 2 == 0)
        result: list[int] = []
        for val, index in queries:
            if nums[index] % 2 == 0:
                even_sum -= nums[index]
            nums[index] += val
            if nums[index] % 2 == 0:
                even_sum += nums[index]
            result.append(even_sum)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n + q) | O(q) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.