985. Sum of Even Numbers After Queries LeetCode Solution
In this guide, you will get 985. Sum of Even Numbers After Queries LeetCode Solution with the best time and space complexity. The solution to Sum of Even Numbers After Queries problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Sum of Even Numbers After Queries solution in C++
- Sum of Even Numbers After Queries solution in Java
- Sum of Even Numbers After Queries solution in Python
- Additional Resources
Problem Statement of 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.
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] <= 104
1 <= queries.length <= 104
-104 <= vali <= 104
0 <= indexi < nums.length
Complexity Analysis
- Time Complexity:
- Space Complexity:
985. Sum of Even Numbers After Queries LeetCode Solution in C++
class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& nums,
vector<vector<int>>& queries) {
vector<int> ans;
int sum =
accumulate(nums.begin(), nums.end(), 0, [](int subtotal, int num) {
return subtotal + (num % 2 == 0 ? num : 0);
});
for (const vector<int>& query : queries) {
const int val = query[0];
const int index = query[1];
if (nums[index] % 2 == 0)
sum -= nums[index];
nums[index] += val;
if (nums[index] % 2 == 0)
sum += nums[index];
ans.push_back(sum);
}
return ans;
}
};
/* code provided by PROGIEZ */
985. Sum of Even Numbers After Queries LeetCode Solution in Java
class Solution {
public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
int[] ans = new int[queries.length];
int sum = 0;
for (final int num : nums)
sum += num % 2 == 0 ? num : 0;
for (int i = 0; i < queries.length; ++i) {
final int val = queries[i][0];
final int index = queries[i][1];
if (nums[index] % 2 == 0)
sum -= nums[index];
nums[index] += val;
if (nums[index] % 2 == 0)
sum += nums[index];
ans[i] = sum;
}
return ans;
}
}
// code provided by PROGIEZ
985. Sum of Even Numbers After Queries LeetCode Solution in Python
class Solution:
def sumEvenAfterQueries(
self,
nums: list[int],
queries: list[list[int]],
) -> list[int]:
ans = []
summ = sum(a for a in nums if a % 2 == 0)
for val, index in queries:
if nums[index] % 2 == 0:
summ -= nums[index]
nums[index] += val
if nums[index] % 2 == 0:
summ += nums[index]
ans.append(summ)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.