Leetcode 1027 Solution

This article provides solution to leetcode question 1027 (sum-of-even-numbers-after-queries)

https://leetcode.com/problems/sum-of-even-numbers-after-queries

Solution

class Solution:
    def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:
        ans = []
        even_sum = sum([a for a in A if a % 2 == 0])

        for val, index in queries:
            old_a = A[index]
            new_a = A[index] + val

            if old_a % 2 == 0:
                even_sum -= old_a
            if new_a % 2 == 0:
                even_sum += new_a

            ans.append(even_sum)
            A[index] = new_a

        return ans