Lets Code Everyday - Day 29

Lets Code Everyday - Day 29

ยท

4 min read

Question - 29:

Sum of Even Numbers After Queries

You are given an integer array nums and an array queries where queries[i] = [val<sub>i</sub>, index<sub>i</sub>].

For each query i, first, apply nums[index<sub>i</sub>] = nums[index<sub>i</sub>] + val<sub>i</sub>, then print the sum of the even values of nums.

Return an integer array answer where answer[i] is the answer to the i<sup>th</sup> 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]

Approach taken:

class Solution {
    public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
        int[] res=new int[nums.length];

        int evenSum=0;

        for(int num:nums){
            if(num%2==0) evenSum=evenSum+num;
        }
        int k=0;
        for(int[] query:queries){
            int val=query[0];
            int index=query[1];
            if(nums[index]%2==0) evenSum-=nums[index];

            nums[index]=nums[index]+val;

            if(nums[index]%2==0) evenSum+=nums[index];

            res[k]=evenSum;
            k++;
        }
     return res;
    }
}

Step-by-step implementation:

  1. Create a public method named sumEvenAfterQueries that takes in an array of integers nums and a 2D array of integers queries. The method returns an array of integers.

  2. Create a new integer array named res with the same length as nums. This array will store the results of the queries.

  3. Create an integer variable named evenSum and set it to zero. This variable will store the sum of all even integers in the nums array.

  4. Loop through each integer in the nums array using a for-each loop. For each integer, check if it is even (i.e., if it is divisible by 2 without a remainder). If it is even, add it to evenSum.

  5. Create an integer variable named k and set it to zero. This variable will keep track of the index in the res array where we will store the result of each query.

  6. Loop through each query in the queries array using a for-each loop. For each query:

    a. Retrieve the value to be added to the integer at the specified index in the nums array (val) and the index itself (index).

    b. If the integer at the specified index in the nums array is even, subtract it from evenSum.

    c. Update the integer at the specified index in the nums array by adding val to it.

    d. If the new integer at the specified index in the nums array is even, add it to evenSum.

    e. Store the current value of evenSum in the res array at index k.

    f. Increment k by 1.

  7. Return the res array.

In summary, the sumEvenAfterQueries method takes an array of integers and a 2D array of integers representing queries. For each query, it updates the value at the specified index in the array by adding a given value, and then calculates the sum of all even integers in the updated array. The method returns an array of integers containing the sum of all even integers after each query.

Conclusion:

The time complexity of the sumEvenAfterQueries method is O(n), where n is the length of the queries array. This is because the method loops through each query in the queries array once, and for each query it performs constant time operations such as array indexing, integer arithmetic, and updating the evenSum variable.

The space complexity of the sumEvenAfterQueries method is O(n), where n is the length of the queries array. This is because the method creates a new integer array res with length n to store the results of the queries, and uses a constant amount of additional space to store integer variables such as evenSum, k, val, and index.

Thanks for reading....Happy Learning...๐Ÿ˜Š

Did you find this article valuable?

Support Vinay Rangaraju by becoming a sponsor. Any amount is appreciated!

ย