Advertisement

Sum of Even Numbers After Queries - LeetCode 985 Solution

Sum of Even Numbers After Queries - Complete Solution Guide

Sum of Even Numbers After Queries is LeetCode problem 985, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array nums and an array queries where queries[i] = [val i , index i ] . For each query i , first, apply nums[index i ] = nums[index i ] + val i , then print the sum of the even values of nums . Return an integer array answer where answer[i] is the answer to the i th 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], an

Detailed Explanation

The problem asks us to process a series of queries on an integer array `nums`. Each query consists of a value `val` and an index `index`. For each query, we must update the element at `nums[index]` by adding `val` to it, i.e., `nums[index] = nums[index] + val`. After each update, we need to compute the sum of all even numbers in the updated `nums` array and store it in the `answer` array. Finally, we return the `answer` array containing the sum of even numbers after each query.

Solution Approach

The provided solution calculates the initial sum of even numbers in the input array `nums`. Then, for each query, it checks if the element at the specified index was even before the update. If it was, it subtracts it from the current even sum. After updating the element, it checks if the updated element is even. If it is, it adds it to the current even sum. Finally, it appends the updated even sum to the `answer` array. This incremental update approach avoids recalculating the entire even sum after each query, leading to a more efficient solution.

Step-by-Step Algorithm

  1. Step 1: Initialize `even_sum` to 0. Iterate through `nums` and add each even number to `even_sum`.
  2. Step 2: Initialize an empty array `answer` to store the results.
  3. Step 3: Iterate through the `queries` array. For each query `[val, index]`:
  4. Step 4: Check if `nums[index]` is even. If it is, subtract `nums[index]` from `even_sum`.
  5. Step 5: Update `nums[index]` by adding `val` to it: `nums[index] += val`.
  6. Step 6: Check if the updated `nums[index]` is even. If it is, add `nums[index]` to `even_sum`.
  7. Step 7: Append `even_sum` to the `answer` array.
  8. Step 8: Return the `answer` array.

Key Insights

  • Insight 1: The key is to efficiently maintain the sum of even numbers. We don't need to recompute the entire sum after each query. We can update the sum incrementally based on whether the updated element was even before and after the query.
  • Insight 2: Modifying the original array `nums` in place allows us to directly see the effect of the query on the array and thus on the even sum.
  • Insight 3: We only need to track whether the element at the given index was even before and after the query. This helps avoid iterating through the entire array after each modification.

Complexity Analysis

Time Complexity: O(n + q)

Space Complexity: O(q)

Topics

This problem involves: Array, Simulation.

Companies

Asked at: Indeed.