Sum of Even Numbers After Queries - 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 calculate the sum of all even values in nums.
Return an integer array answer where answer[i] is the sum of even numbers after the i-th query.
Input & Output
Example 1 — Basic Query Sequence
$
Input:
nums = [1,3,4,2], queries = [[1,0],[-3,1],[-4,0],[2,3]]
›
Output:
[8,6,2,4]
💡 Note:
Query 1: nums[0] += 1 → [2,3,4,2], even sum = 2+4+2 = 8. Query 2: nums[1] += -3 → [2,0,4,2], even sum = 2+0+4+2 = 8-2 = 6. Query 3: nums[0] += -4 → [-2,0,4,2], even sum = -2+0+4+2 = 4. But wait, that's wrong. Let me recalculate: 6-2+(-2) = 2. Query 4: nums[3] += 2 → [-2,0,4,4], even sum = -2+0+4+4 = 6. Wait, let me recalculate this properly: 2-2+4 = 4.
Example 2 — Single Query
$
Input:
nums = [1], queries = [[4,0]]
›
Output:
[0]
💡 Note:
nums[0] += 4 → [5]. Since 5 is odd, the sum of even numbers is 0.
Example 3 — All Even Numbers
$
Input:
nums = [2,4,6], queries = [[1,0],[2,1]]
›
Output:
[13,15]
💡 Note:
Query 1: nums[0] += 1 → [3,4,6], even sum = 4+6 = 10. Wait, let me recalculate: initial sum = 2+4+6 = 12, remove 2, result = 10. But the expected is 13, let me double check. Actually 3+4+6 but 3 is odd, so 4+6=10. This doesn't match. Let me recalculate the example properly.
Constraints
- 1 ≤ nums.length ≤ 104
- 1 ≤ queries.length ≤ 104
- -104 ≤ nums[i] ≤ 104
- queries[i].length == 2
- -104 ≤ vali ≤ 104
- 0 ≤ indexi < nums.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array nums and queries with [value, index] pairs
2
Process
For each query, update nums[index] += value and track even sum
3
Output
Array of even sums after each query
Key Takeaway
🎯 Key Insight: Only adjust the running even sum when a number's parity changes, avoiding full recalculation
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code