Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Amazon
Microsoft
Sum Of Even Numbers After Queries

Maintain the sum of even numbers in an array while applying a sequence of value-update queries.

Acceptance 0%
Problem Statement

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] meaning nums[index] += val.

Return the even-sum after each query.

Output Format

  • An array ans where ans[i] is the sum of all even numbers in nums after processing the i-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 O(n+q)O(n + q) or similar efficient solution rather than recomputing the full sum after every update.
Examples
Sample cases returned by the problem API.

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.

  1. nums[0] becomes 2 -> even sum = 2 + 2 + 4 = 8
  2. nums[1] becomes -1 -> even sum = 2 + 4 = 6
  3. nums[0] becomes -2 -> even sum = -2 + 4 = 2
  4. 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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.