Leetcode 985: Sum of Even Numbers After Queries

grid47
grid47
Exploring patterns and algorithms
Jul 31, 2024 5 min read

Given an integer array nums and an array of queries, each of which is in the form [value, index], you need to apply each query by adding value to nums[index] and return the sum of even numbers in the updated nums array after each query.
Problem
Approach
Steps
Complexity
Input: You are given an array nums of integers and an array of queries, where each query consists of a value to be added to nums at a specific index.
Example: nums = [5, 7, 9, 10], queries = [[2, 0], [-5, 1], [4, 0], [6, 3]]
Constraints:
• 1 <= nums.length <= 10^4
• -10^4 <= nums[i] <= 10^4
• 1 <= queries.length <= 10^4
• -10^4 <= value <= 10^4
• 0 <= index < nums.length
Output: For each query, return the sum of even numbers in nums after the query has been applied.
Example: Output: [12, 7, 12, 14]
Constraints:
• The output should be an array of integers representing the sum of even numbers after each query.
Goal: The goal is to modify the nums array with each query and compute the sum of even values in the updated array.
Steps:
• Initialize a sum variable to hold the sum of even numbers in nums.
• Iterate over each query and modify the appropriate value in nums.
• After modifying nums, update the sum of even numbers based on the updated value.
Goal: The problem must be solved efficiently given the constraints on the array and queries sizes.
Steps:
• nums.length and queries.length can be as large as 10^4, so the solution must be efficient.
Assumptions:
• Each query modifies nums at a specific index, and the sum of even values is updated immediately after each modification.
Input: nums = [5, 7, 9, 10], queries = [[2, 0], [-5, 1], [4, 0], [6, 3]]
Explanation: In this example, we modify nums in place for each query and compute the sum of even values after each query.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus