You are given an integer array nums
and an array queries
where each queries[i] = [val, index]
. For each query, you are to add val
to nums[index]
and then record the sum of the even numbers in nums
after the update.
Return an array of the answers to each query, in the same order as the queries.
i
-th element is the sum of even numbers in nums
after applying the i
-th query.nums
and queries
can be up to 104.Key Points:
nums
.nums
.
At first glance, we might think to process each query by updating the relevant element in nums
and then scanning the entire array to sum the even numbers. However, this approach is inefficient because it would require O(n) time per query, leading to O(n*q) total time for n
elements and q
queries. With large inputs, this quickly becomes too slow.
To optimize, we notice that each query only changes one value in the array. The only way the sum of even numbers changes is if the updated value was even before, or becomes even after. This means we don't need to rescan the whole array; we can keep a running total of the sum of evens and update it intelligently after each query.
By focusing only on the changed element, we can make our solution much more efficient.
We'll follow these steps:
nums
and store it in a variable, say even_sum
.
[val, index]
:
nums[index]
is even before the update, subtract it from even_sum
(since it may change to odd after the update).val
to nums[index]
.nums[index]
is even after the update, add it back to even_sum
(it is now an even number contributing to the sum).even_sum
to the result array.This approach ensures we only ever touch one element per query and keep the sum updated efficiently.
Input:
nums = [1,2,3,4]
queries = [[1,0],[-3,1],[-4,0],[2,3]]
Step-by-step:
Brute-force approach:
nums
.nums
.The optimized approach is much more efficient, especially for large inputs.
We tackled the "Sum of Even Numbers After Queries" problem by recognizing that only the changed element in each query can affect the sum of even numbers. By keeping a running total and updating it smartly based on whether the updated element is even or odd before and after the change, we reduced the time complexity from O(n*q) to O(n+q). This makes the solution efficient and elegant, suitable for large input sizes.
class Solution:
def sumEvenAfterQueries(self, nums, queries):
even_sum = sum(x for x in nums if x % 2 == 0)
result = []
for val, idx in queries:
if nums[idx] % 2 == 0:
even_sum -= nums[idx]
nums[idx] += val
if nums[idx] % 2 == 0:
even_sum += nums[idx]
result.append(even_sum)
return result
class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& nums, vector<vector<int>>& queries) {
int even_sum = 0;
for (int x : nums) {
if (x % 2 == 0) even_sum += x;
}
vector<int> result;
for (const auto& q : queries) {
int val = q[0], idx = q[1];
if (nums[idx] % 2 == 0) even_sum -= nums[idx];
nums[idx] += val;
if (nums[idx] % 2 == 0) even_sum += nums[idx];
result.push_back(even_sum);
}
return result;
}
};
class Solution {
public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
int evenSum = 0;
for (int x : nums) {
if (x % 2 == 0) evenSum += x;
}
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int val = queries[i][0], idx = queries[i][1];
if (nums[idx] % 2 == 0) evenSum -= nums[idx];
nums[idx] += val;
if (nums[idx] % 2 == 0) evenSum += nums[idx];
result[i] = evenSum;
}
return result;
}
}
var sumEvenAfterQueries = function(nums, queries) {
let evenSum = nums.reduce((acc, x) => x % 2 === 0 ? acc + x : acc, 0);
let result = [];
for (let [val, idx] of queries) {
if (nums[idx] % 2 === 0) evenSum -= nums[idx];
nums[idx] += val;
if (nums[idx] % 2 === 0) evenSum += nums[idx];
result.push(evenSum);
}
return result;
};