- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Sum of Even Numbers After Queries in Python

Suppose we have an array of integers called A, and an array queries. For the i-th query value = queries[i][0] and index = queries[i][1], we will add value to A[index]. Then, the answer of the i-th query is the sum of the even values of A. We have to find the answer to all queries. We will find an array, that should have answer[i] as the answer to the i-th query. So if the array is like [1,2,3,4], and the query array is like [[1,0],[-3,1],[-4,0],[2,3]], then the answer array will be like [8,6,2,4]. So at first the array is [1,2,3,4], then after the first query, add 1 with A[0], then array will be [2,2,3,4], the sum of even values are 2 + 2 + 4 = 8. For the second query, it will add -3 with A[1], then the array will be [2,-1,3,4], so sum of even numbers 2 + 4 = 6. Like that we are getting the array [8,6,2,4]

To solve this, we will follow these steps −

- Define an array named res to store results
- sum := 0
- for each element i in A
- if i is even, then sum := sum + i

- for each query i in queries −
- index := i[1]
- val := i[0]
- if A[index] is even, then sum := sum – A[index]
- A[index] := A[index] + val
- if A[index] is even, then sum := sum + A[index]
- sum is appended to the res

- return res

## Example

Let us see the following implementation to get better understanding −

class Solution(object): def sumEvenAfterQueries(self, A, queries): result = [] sum = 0 for i in A: if i%2==0: sum+=i for i in queries: index = i[1] val = i[0] if A[index] % 2==0: sum-=A[index] A[index]+=val if A[index]%2==0: sum+=A[index] result.append(sum) return result ob1 = Solution() print(ob1.sumEvenAfterQueries([1,2,3,4], [[1,0],[-3,1],[-4,0],[2,3]]))

## Input

[1,2,3,4] [[1,0],[-3,1],[-4,0],[2,3]]

## Output

[8,6,2,4]

- Related Articles
- Program to find kpr sum for all queries for a given list of numbers in Python
- Find the sum of first and even natural numbers.
- Find the Initial Array from given array after range sum queries in C++
- Sum of even numbers up to using recursive function in JavaScript
- Sum of squares of the first n even numbers in C Program
- Haskell Program to calculate the sum of all even numbers
- Golang program to calculate the sum of all even numbers
- What is the sum of any two (a) Odd numbers?(b) Even numbers?
- 8085 program to find the sum of series of even numbers
- Find Numbers with Even Number of Digits in Python
- 8086 program to find sum of Even numbers in a given series
- Array sum after dividing numbers from previous?
- The sum of the squares of two consecutive even numbers is 340. Find the numbers.
- Swift Program to calculate the sum of first N even numbers
- Maximum subarray sum after dividing array into subarrays based on the given queries in Java