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.
Example 1:
Input: nums = [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 nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8. After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6. After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2. After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
Example 2:
Input: nums = [1], queries = [[4,0]] Output: [0]
Constraints:
1 <= nums.length <= 104-104 <= nums[i] <= 1041 <= queries.length <= 104-104 <= vali <= 1040 <= indexi < nums.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Apply each update directly to the array, then walk the whole array again and add up every even value. That fresh total is the answer for the query. Repeat this for every query, in order.
Here's how the algorithm would work step-by-step:
class Solution:
def sumEvenAfterQueries(self, numbers_list: list[int], queries: list[list[int]]) -> list[int]:
answers_for_each_query = []
# Process each query one by one as described in the instructions.
for value_to_add, index_to_modify in queries:
numbers_list[index_to_modify] += value_to_add
current_even_sum = 0
# After each modification, we must recalculate the sum of even numbers from scratch.
for number in numbers_list:
# Check if the number is even to decide if it should be included in our sum.
if number % 2 == 0:
current_even_sum += number
answers_for_each_query.append(current_even_sum)
return answers_for_each_queryKeep one running total of the even numbers. Before an update, remove the old value from that total if it was even. After the update, add the new value if it is even. Each query then reports the total without scanning the array again.
Here's how the algorithm would work step-by-step:
def sum_even_after_queries(numbers, queries):
# First, calculate the initial sum of all even numbers in the array.
current_even_sum = sum(number for number in numbers if number % 2 == 0)
answer_list = []
for value_to_add, index_to_modify in queries:
# If the original number is even, it must be removed from the sum before modification.
if numbers[index_to_modify] % 2 == 0:
current_even_sum -= numbers[index_to_modify]
# Perform the actual modification on the number in the array.
numbers[index_to_modify] += value_to_add
# If the new, modified number is even, it should be included in the running sum.
if numbers[index_to_modify] % 2 == 0:
current_even_sum += numbers[index_to_modify]
answer_list.append(current_even_sum)
return answer_list| Case | How to Handle |
|---|---|
| The array starts with no even numbers and a query creates one | The new even value is included in the sum for that query. |
| An even number becomes odd | That value leaves the even sum on the query that changes it. |
| An odd number becomes even | That value joins the even sum after the update. |
| A value is zero | Zero is even, so it counts toward the sum. |
| A negative even number is present | It is still even, so it is included in the sum. |