1
class Solution {
2
public:
3
vector<int> sumEvenAfterQueries(vector<int> &nums, vector<vector<int>> &queries) {
4
// Sum of all even numbers in the array intially
5
int tes = accumulate(nums.begin(), nums.end(), 0, [](int curr, int a) {
6
if (a % 2 == 0)
7
return curr += a;
8
else
9
return curr;
10
});
11
vector<int> ans(queries.size());
12
bool a = false, b = false;
13
for (int i = 0; i < queries.size(); i++) {
14
// If both are even or odd it stays even then
15
a = nums[queries[i][1]] % 2 == 0 && queries[i][0] % 2 == 0;
16
b = abs(nums[queries[i][1]] % 2) == 1 && abs(queries[i][0] % 2) == 1;
17
if (a)
18
tes += queries[i][0];
19
else if (b)
20
tes += queries[i][0] + nums[queries[i][1]];
21
else if (nums[queries[i][1]] % 2 == 0)
22
tes -= nums[queries[i][1]]; // If even turns to odd because of operation
23
// remove that even value from ans
24
ans[i] = tes; // Adding the result
25
nums[queries[i][1]] += queries[i][0]; // now completing the operation in queries to use in
26
// further operations
27
}
28
return ans;
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0