Sum of Even Numbers After Queries - Array - Easy - LeetCode
💻 coding

Sum of Even Numbers After Queries - Array - Easy - LeetCode

3 min read 528 words
3 min read
ShareWhatsAppPost on X
  • 1The algorithm processes an array of integers and a series of queries to calculate the sum of even numbers after each modification.
  • 2Each query updates a specific index in the array and recalculates the sum of even values, which is returned in an output array.
  • 3The solution operates with a time complexity of O(n) and a space complexity of O(1), making it efficient for large input sizes.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The algorithm processes an array of integers and a series of queries to calculate the sum of even numbers after each modification."

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.)

Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.

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;

namespace LeetCode.AskGif.Easy.Array
{
 public class SumEvenAfterQueriesSoln
 {
 public int[] SumEvenAfterQueries(int[] A, int[][] queries)
 {
 var res = new int[A.Length];
 var sumEven = 0;
 int add = 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++)
 {
 add = A[queries[i][1]] + queries[i][0];

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

 //update new value in sumEven
 if(add % 2 == 0)
 {
 sumEven += add;
 }

 //update array
 A[queries[i][1]] = add;

 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;
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 22 June 2020 · 3 min read · 528 words

Part of AskGif Blog · coding

You might also like