Problem
You are given an integer array nums
and an array queries
where queries[i] = [vali, indexi]
.
For each query i
, first, apply nums[indexi] = nums[indexi] + vali
, then print the sum of the even values of nums
.
Return an integer array answer
where answer[i]
is the answer to the ith
query.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - 2 Pass with Updation of Even Sums
- Calculate the sum of even numbers in array
- Now we go through queries.
- For each query, we calculate the new number’s value
- We check if new number’s value is even. If yes, we check if original number was even. If yes, then we just increment by query value, else we just add new value, as this number converted from odd to even
- Else if new number is not even, and existing number was even, just remove it from evenSum, as it was even earlier, but now odd.
Code
|
|
|
|
|
|
Complexity
- Time:
O(n)
- Space:
O(1)