Algorithm


Problem Name: 985. Sum of Even Numbers After Queries

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

For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.

Return an integer array answer where answer[i] is the answer to the ith query.

 

Example 1:

Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
Explanation: At the beginning, the array is [1,2,3,4].
After adding 1 to nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Example 2:

Input: nums = [1], queries = [[4,0]]
Output: [0]

 

Constraints:

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

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
    int evenSum = 0;
    for (int num : nums) {
      evenSum += num % 2 == 0 ? num : 0;
    }
    int[] result = new int[nums.length];
    for (int i = 0; i  <  queries.length; i++) {
      int idx = queries[i][1];
      if (nums[idx] % 2 == 0) {
        evenSum -= nums[idx];
      }
      nums[idx] += queries[i][0];
      if (nums[idx] % 2 == 0) {
        evenSum += nums[idx];
      }
      result[i] = evenSum;
    }
    return result;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]

Output

x
+
cmd
[8,6,2,4]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const sumEvenAfterQueries = function(A, queries) {
    let sum = A.reduce((acc, cur) => cur%2 == 0 ? acc + cur : acc, 0);
    return queries.map((q) => {
        let i = q[1];
        let s = A[i] + q[0];
        if(s%2 === 0) {
            sum += q[0];
            if(A[i]%2 !== 0) sum += A[i];
        } else if(A[i]%2 === 0) sum -= A[i];
        A[i] = s;
        return sum;
    });
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]

Output

x
+
cmd
[8,6,2,4]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:
        sm = sum(a for a in A if a % 2 == 0)
        for i in range(len(queries)):
            val, ind = queries[i]
            sm -= A[ind] % 2 == 0 and A[ind]
            A[ind] += val
            sm += A[ind] % 2 == 0 and A[ind]
            queries[i] = sm
        return queries
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1], queries = [[4,0]]

Output

x
+
cmd
[0]

#4 Code Example with C# Programming

Code - C# Programming


using System.Linq;

namespace LeetCode
{
    public class _0985_SumOfEvenNumbersAfterQueries
    {
        public int[] SumEvenAfterQueries(int[] A, int[][] queries)
        {
            var sum = A.Where(num => num % 2 == 0).Sum();
            var result = new int[queries.Length];

            for (int i = 0; i  <  queries.Length; i++)
            {
                int val = queries[i][0], index = queries[i][1];
                if (A[index] % 2 == 0)
                    sum -= A[index];

                A[index] += val;

                if (A[index] % 2 == 0)
                    sum += A[index];

                result[i] = sum;
            }

            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1], queries = [[4,0]]

Output

x
+
cmd
[0]
Advertisements

Demonstration


Previous
#984 Leetcode String Without AAA or BBB Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#986 Leetcode Interval List Intersections Solution in C, C++, Java, JavaScript, Python, C# Leetcode