Sum of Even Numbers After Queries

Medium
1838.

Sum of Even Numbers After Queries

Sum of Even Numbers After Queries is a medium coding problem on arrays and simulation, asked in Amazon and Indeed interviews.

You are given an integer array nums and an array queries where queries[i] = [val, index].

Process the queries in order. For query i, first add val to nums[index], then record the sum of the even values currently in nums.

Return the recorded sums as an array, one per query.

Example 1

Input
nums = [2,5,6,1], queries = [[3,1],[-2,0],[1,3],[-6,2]]
Output
[16,14,16,10]
Explanation
The array becomes [2,8,6,1] (sum 16), [0,8,6,1] (14), [0,8,6,2] (16), [0,8,0,2] (10).

Example 2

Input
nums = [7], queries = [[4,0]]
Output
[0]
Explanation
`7 + 4 = 11` is odd, so no even value remains.

Example 3

Input
nums = [-2,3], queries = [[1,1],[-1,0]]
Output
[2,4]
Explanation
Negative even numbers count too: `-2 + 4 = 2`, then `-2` becomes `-3`, leaving `4`.

Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • 1 <= queries.length <= 10^4
  • -10^4 <= val <= 10^4
  • 0 <= index < nums.length
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @param {number[][]} queries
 * @return {number[]}
 */
var sumEvenAfterQueries = function(nums, queries) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Sum of Even Numbers After Queries — Problem & Solution

Sum of Even Numbers After Queries solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.

Sum of Even Numbers After Queries is a medium coding problem on arrays and simulation, asked in Amazon and Indeed interviews.

  • Difficulty: Medium
  • Topics: Arrays, Simulation
  • Asked at: Amazon, Indeed
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given an integer array nums and an array queries where queries[i] = [val, index].

Process the queries in order. For query i, first add val to nums[index], then record the sum of the even values currently in nums.

Return the recorded sums as an array, one per query.

Example 1

Input: nums = [2,5,6,1], queries = [[3,1],[-2,0],[1,3],[-6,2]]
Output: [16,14,16,10]
Explanation: The array becomes [2,8,6,1] (sum 16), [0,8,6,1] (14), [0,8,6,2] (16), [0,8,0,2] (10).

Example 2

Input: nums = [7], queries = [[4,0]]
Output: [0]
Explanation: `7 + 4 = 11` is odd, so no even value remains.

Example 3

Input: nums = [-2,3], queries = [[1,1],[-1,0]]
Output: [2,4]
Explanation: Negative even numbers count too: `-2 + 4 = 2`, then `-2` becomes `-3`, leaving `4`.

Constraints

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

Hints for Sum of Even Numbers After Queries

  1. Hint 1

    Recomputing the even sum after every query is O(n) per query — 10^8 steps at the limits. Only one element changes per query.

  2. Hint 2

    Keep the running even sum. Before changing nums[index], subtract it from the sum if it is even; after changing it, add it back if it is now even.

How to solve Sum of Even Numbers After Queries

Maintain the even sum incrementally: a query changes one element, so only that element's contribution to the sum can change.

Approach

  1. Compute even = the sum of the even values of nums.
  2. For each query [val, index]: if nums[index] is even, subtract it from even.
  3. Add val to nums[index]; if the new value is even, add it to even.
  4. Record even.

Why it works

Before the update, even counts nums[index] exactly when it is even, so removing that contribution leaves the even sum of all other elements, which the query does not touch. Adding the new value back when it is even restores the definition.

Complexity

  • Time — O(n + q)
  • Space — O(1) besides the output

Pitfalls

  • Negative numbers: -3 % 2 is -1 in most languages, so test evenness with % 2 == 0, never % 2 == 1 for odd.
  • Remove the old contribution before applying val.

Reference solution

Python

from typing import List

def sumEvenAfterQueries(nums: List[int], queries: List[List[int]]) -> List[int]:
    even = sum(v for v in nums if v % 2 == 0)
    out = []
    for val, idx in queries:
        if nums[idx] % 2 == 0:
            even -= nums[idx]
        nums[idx] += val
        if nums[idx] % 2 == 0:
            even += nums[idx]
        out.append(even)
    return out

Java

public static int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
    int even = 0;
    for (int v : nums) if (v % 2 == 0) even += v;
    int[] out = new int[queries.length];
    for (int q = 0; q < queries.length; q++) {
        int val = queries[q][0], idx = queries[q][1];
        if (nums[idx] % 2 == 0) even -= nums[idx];
        nums[idx] += val;
        if (nums[idx] % 2 == 0) even += nums[idx];
        out[q] = even;
    }
    return out;
}

C++

vector<int> sumEvenAfterQueries(vector<int>& nums, vector<vector<int>>& queries) {
    int even = 0;
    for (int v : nums) if (v % 2 == 0) even += v;
    vector<int> out;
    for (auto& q : queries) {
        int val = q[0], idx = q[1];
        if (nums[idx] % 2 == 0) even -= nums[idx];
        nums[idx] += val;
        if (nums[idx] % 2 == 0) even += nums[idx];
        out.push_back(even);
    }
    return out;
}

JavaScript

var sumEvenAfterQueries = function(nums, queries) {
    var even = 0;
    for (var i = 0; i < nums.length; i++) if (nums[i] % 2 === 0) even += nums[i];
    var out = [];
    for (var q = 0; q < queries.length; q++) {
        var val = queries[q][0], idx = queries[q][1];
        if (nums[idx] % 2 === 0) even -= nums[idx];
        nums[idx] += val;
        if (nums[idx] % 2 === 0) even += nums[idx];
        out.push(even);
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays