Ctrl K

Even Total After Edits

Medium
You get an array values and a list of edits updates, where each edit is a pair [delta, index]: add delta to values[index]. Apply the edits in order. After each edit, record the sum of all even elements of the array (negative even numbers count too). Return the list of these sums. Re-summing the whole array after every edit is too slow; keep the total up to date in O(1).
Example 1
values=[5, 6, 7], updates=[[1, 0], [3, 1], [-7, 2]] → [12, 6, 6]

After [1, 0] the array is [6, 6, 7], even total 12. After [3, 1] it is [6, 9, 7], total 6. After [-7, 2] it is [6, 9, 0], total 6 + 0 = 6. The answer is [12, 6, 6].

Example 2
values=[-3], updates=[[1, 0], [1, 0]] → [-2, 0]

First −3 + 1 = −2 (even, total −2), then −2 + 1 = −1 (odd, total 0).

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁴ ≤ values[i] ≤ 10⁴
  • 1 ≤ len(updates) ≤ 10⁵
  • -10⁴ ≤ delta ≤ 10⁴
  • 0 ≤ index < len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
values=[5, 6, 7], updates=[[1, 0], [3, 1], [-7, 2]]
Expected
[12, 6, 6]

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.