985. Sum of Even Numbers After Queries
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& nums, vector<vector<int>>& queries) { vector<int> res; int sum = 0; for(auto& n : nums) { if(n % 2 == 0) sum += n; } for(auto& q : queries) { int v = q[0], idx = q[1]; if(nums[idx] % 2 == 0) sum -= nums[idx]; nums[idx] += v; if(nums[idx] % 2 == 0) sum += nums[idx]; res.push_back(sum); } return res; } };
|