# Blogs Hub

### by Sumit Chourasia | Jun 22, 2020 | Category :coding | Tags : आसानarrayडेटा-संरचनाleetcodec-sharpalgorithm

#### Sum of Even Numbers After Queries - Array - Easy - LeetCode - मिनी टीवी

We have an array A of integers, and an array queries of queries.

For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum of the even values of A.

(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)

Example 1:

Input: A = [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 A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.

After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.

After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.

After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Note:

1 <= A.length <= 10000

-10000 <= A[i] <= 10000

1 <= queries.length <= 10000

-10000 <= queries[i][0] <= 10000

0 <= queries[i][1] < A.length

Solution:

``````using System;
using System.Collections.Generic;
using System.Text;

{
public class SumEvenAfterQueriesSoln
{
public int[] SumEvenAfterQueries(int[] A, int[][] queries)
{
var res = new int[A.Length];
var sumEven = 0;
for (int i = 0; i < A.Length; i++)
{
if(A[i] % 2 == 0)
{
sumEven += A[i];
}
}

for (int i = 0; i < A.Length; i++)
{

//delete old value in sumEven
if (A[queries[i][1]] % 2 == 0)
{
sumEven -= A[queries[i][1]];
}

//update new value in sumEven
{
}

//update array

res[i] = sumEven;
}

return res;
}
}
}
``````

Time Complexity: O(n)

Space Complexity: O(1)

Unit Tests:

``````using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
[TestClass]
public class SumEvenAfterQueriesSolnTests
{
[TestMethod]
public void SumEvenAfterQueriesSoln_First()
{
var A = new int[] { 1, 2, 3, 4 };
var queries = new int[,] {
{1, 0 },
{-3, 1 },
{-4, 0 },
{ 2, 3 }
};
var expected = new int[] { 8, 6, 2, 4 };

var res = new SumEvenAfterQueriesSoln().SumEvenAfterQueries(A, ArrayMapper(queries));
AreEqual(expected, res);
}

[TestMethod]
public void SumEvenAfterQueriesSoln_Second()
{
var A = new int[] { 5, 5, 4 };
var queries = new int[,] {
{0, 1 },
{1, 0 },
{ 4, 1 }
};
var expected = new int[] { 4, 10, 10 };

var res = new SumEvenAfterQueriesSoln().SumEvenAfterQueries(A, ArrayMapper(queries));
AreEqual(expected, res);
}

private void AreEqual(int[] res, int[] output)
{
Assert.AreEqual(res.Length, output.Length);
for (int i = 0; i < res.Length; i++)
{
Assert.AreEqual(res[i], output[i]);
}
}

private int[][] ArrayMapper(int[,] matrix)
{
var arr = new int[matrix.GetLength(0)][];
for (int i = 0; i < matrix.GetLength(0); i++)
{
arr[i] = new int[matrix.GetLength(1)];
for (int j = 0; j < matrix.GetLength(1); j++)
{
arr[i][j] = matrix[i, j];
}
}

return arr;
}
}
}
``````

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia