Maintain the sum of even numbers in an array while applying a sequence of value-update queries.
You are given an integer array and a list of update queries. Each query adds a value to one position in the array. After applying each query, return the sum of all even numbers currently in the array.
The key challenge is to avoid recomputing the even-sum from scratch after every update. Track how the affected element changes and update the running total efficiently.
Input Format
- An integer array
nums. - A list of queries, where each query is of the form
[val, index]meaningnums[index] += val.
Return the even-sum after each query.
Output Format
- An array
answhereans[i]is the sum of all even numbers innumsafter processing thei-th query.
Constraints
- The array may contain positive, zero, or negative integers.
- Each query updates exactly one index.
- A query may change an element from even to odd, odd to even, or keep the parity unchanged.
- Aim for an or similar efficient solution rather than recomputing the full sum after every update.
Example 1
Input
nums = [1,2,3,4] queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output
[8,6,2,4]
Explanation
Start with even sum = 2 + 4 = 6.
- nums[0] becomes 2 -> even sum = 2 + 2 + 4 = 8
- nums[1] becomes -1 -> even sum = 2 + 4 = 6
- nums[0] becomes -2 -> even sum = -2 + 4 = 2
- nums[3] becomes 6 -> even sum = -2 + 6 = 4
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.