VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-the-sum-of-array-after-doing-k-queries-on-the-array/

⇱ Print the sum of array after doing k queries on the array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print the sum of array after doing k queries on the array

Last Updated : 10 Feb, 2023

Given an array, arr[] of size n. Calculate the sum of arr[] by performing k queries on arr[], such that we have to add an element given in array y[], on the basis of the boolean value given in array check[]. 

For every index i (0<= i <= n - 1):

  • If check[i] = true, Add y[i] to all the odd numbers present in the array.
  • If check[i] = false, Add y[i] to all the even numbers present in the array.

Examples:

Input: n = 3, k = 3, arr[] = {1, 2, 4}, check[] = {false, true, false}, y[] = {2, 3, 5}
Output: 29
Explanation:

  • for, k = 1
    check[0] = false, y[0] = 2
    So, as per operation add to the even number
    arr = [1, 2+2, 4+2] = {1, 4, 6}
  • for, k = 2
    check[1] = true, y[1] = 3
    so, add to the odd number
    arr = [1+3, 4, 6] = {4, 4, 6}
  • for, k = 3
    check[2] = false, y[2] = 5
    so, add to the even number
    arr = [4+5, 4+5, 6+5] = {9, 9, 11}

So, finally, print the sum of the array = 9 + 9 + 11 = 29

Input: n = 2, k = 1, arr[] = {1, 2}, check[] = {false}, y[] = {0}
Output: 3
Explanation: As check[0] = false, we have to y[0] to all the even elements present in the array. Therefore, the final sum is 1 + 2 =3.

Approach: The basic idea to solve the above problem is:

Iterate through whole the array arr[] for k times, check whether the element present is odd or even, and add an element accordingly. 

Time complexity: O(k*n), where k = no of queries , n = length of array
Auxiliary Space: O(1)

Efficient Approach: The above idea can be optimized as:

Iterate over the arr[], count the number of even and odd elements present. Now, Run a loop for k queries along by keep a check on array check[]. If check[i] == true, Add the total sum by count odd * y[i], otherwise if it's false, add  count even * y[i]. Also, change the count of  even and odd according to y[i], if it's odd. 

Steps involved in the implementation of the code:

Step 1: First calculate the total sum of the array.

Step 2: count of number of odd and even elements in the array.

Step 3: Run the loop for the k queries.

Step 4: If (check[first loop index] = true) then, 

  • Update the total sum => total sum += (count odd * y[first loop index])
  • If the number which is added y[first loop index] is odd then,
  • Update even count => count even += odd count, and  odd count = 0

Step 5: If(check[first loop index] = false) then,

  • Update the total sum => total sum += (count even* y[first loop index])
  • If the number which is added y[first loop index] is odd then, 
  • Update odd count => odd count += even count, and even count = 0

Step 6: Finally print the total sum.

Below is the implementation of the above approach:


Output
29
196

Time Complexity: O(max(n,  k)), where n = size of array, k = no of queries
Auxiliary Space: O(1)

Comment