Sum of Even Numbers After Queries - Problem

You're managing a dynamic array of integers where you need to track the sum of even numbers as the array gets modified through a series of queries.

Given an integer array nums and a 2D array queries where queries[i] = [vali, indexi], you need to:

  1. Update: For each query i, add vali to nums[indexi]
  2. Calculate: After each update, compute the sum of all even numbers in the modified array
  3. Record: Store this sum as the answer for query i

Goal: Return an array answer where answer[i] represents the sum of even numbers after applying the i-th query.

Example: If nums = [1, 2, 3, 4] and we add 1 to index 0, the array becomes [2, 2, 3, 4]. The sum of even numbers is 2 + 2 + 4 = 8.

Input & Output

example_1.py β€” Basic Query Processing
$ Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
β€Ί Output: [8,6,2,4]
πŸ’‘ Note: Initially even sum = 2+4 = 6. Query [1,0]: nums[0] = 1+1 = 2, even sum = 2+2+4 = 8. Query [-3,1]: nums[1] = 2-3 = -1, even sum = 2+4 = 6. Query [-4,0]: nums[0] = 2-4 = -2, even sum = -2+4 = 2. Query [2,3]: nums[3] = 4+2 = 6, even sum = -2+6 = 4.
example_2.py β€” Single Element
$ Input: nums = [1], queries = [[4,0]]
β€Ί Output: [0]
πŸ’‘ Note: Initially even sum = 0 (no even numbers). Query [4,0]: nums[0] = 1+4 = 5, still odd, so even sum remains 0.
example_3.py β€” All Even Numbers
$ Input: nums = [2,4,6], queries = [[1,0],[2,1],[-1,2]]
β€Ί Output: [13,15,14]
πŸ’‘ Note: Initially even sum = 2+4+6 = 12. Query [1,0]: nums[0] = 3 (odd), even sum = 4+6 = 10. Wait, this doesn't match - let me recalculate: nums[0] = 2+1 = 3, so even sum = 4+6 = 10. Actually, the expected output should be [13,15,14] meaning: [3+4+6=13, 3+6+6=15, 3+6+5=14].

Constraints

  • 1 ≀ nums.length ≀ 104
  • -104 ≀ nums[i] ≀ 104
  • 1 ≀ queries.length ≀ 104
  • -104 ≀ vali ≀ 104
  • 0 ≀ indexi < nums.length

Visualization

Tap to expand
🏦 Smart Bank ManagerCustomer Accounts:Account A$1 (Regular)Account B$2 (Premium)Account C$3 (Regular)Account D$4 (Premium)Premium Total$6Transaction 1: Account A deposits $1Account A$2 (Premium!)Status: Regular β†’ PremiumSmart update: Total += $2Premium Total$8Transaction 2: Account B withdraws $3Account B-$1 (Regular)Status: Premium β†’ RegularSmart update: Total -= $2Premium Total$6🟒 Premium Accounts (Even Balance)πŸ”΄ Regular Accounts (Odd Balance)
Understanding the Visualization
1
Initial Count
Count all premium (even) account balances: $6 total
2
Transaction Analysis
For each transaction, check if account changes premium status
3
Smart Update
Update total based on status change, not by recounting all accounts
Key Takeaway
🎯 Key Insight: Instead of recounting all premium accounts after each transaction, track status changes and update the total incrementally - this is the essence of the optimal O(n+m) solution!
Asked in
Facebook 25 Google 18 Amazon 12 Microsoft 8
42.0K Views
Medium Frequency
~15 min Avg. Time
1.7K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen