VOOZH about

URL: https://www.geeksforgeeks.org/dsa/update-and-print-for-all-the-query/

⇱ Update and Print for all the Query - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Update and Print for all the Query

Last Updated : 23 Jul, 2025

Given an array arr[] of length N consisting of a positive integer, the task is to complete the Q queries and print values accordingly which consists of two types of operations:

  • Operation 1: Update all the indices of the array to its digit sum which lies under the range L and R(0 ? L, R ? N-1).
  • Operation 2: Print the value of the array at that index.

Examples:

Input: N = 5, arr[] = {1, 420, 69, 1434, 2023}, Q = 8, queries[][] = [[1, 1, 2], [2, 1], [2, 2], [2, 3], [1, 1, 4], [2, 0], [2, 2], [2, 4]]
Output: [6, 15, 1434, 1, 6, 7]
Explanation:  In the beginning, arr[] = [1, 420, 69, 1434, 2023]

  • 1st query is [1, 1, 2] means operation 1 is used so arr[1] = 4 + 2 + 0 = 6 and arr[2] = 6 + 9 = 15, after updating array arr = [1, 6, 15, 1434, 2023]
  • 2nd query is [2, 1] means operation 2 is used and we need to print arr[1] = 6
  • 3rd query is [2, 2] so, arr[2] = 15
  • 4th query is [2, 3] so, arr[3] = 1434
  • 5th query is [1, 1, 4] so, after updating the array arr = [1, 6, 6, 12, 7]
  • 6th query is [2, 0] so, arr[0] = 1
  • 7th query is [2, 2] so, arr[2] = 6
  • 8th query is [2, 4] so, arr[4] = 7

So in the final, we'll return [6, 15, 1434, 1, 6, 7]

Approach: The problem can be solved based on the following idea:

We will use Segment Tree to solve this problem:

For input arr = [1, 420, 69, 1434, 2023] (N = 5)
Digitsum of every element till it is greater equal to 10

  • 1 -> [1]
  • 420 -> [420, 6]
  • 69 -> [69, 15, 6]
  • 1434 -> [1434, 12, 3]
  • 2023 -> [2023, 7]

Initially, we will make segment tree for the given array in which the nodes contains value 0.

                (1, 0, 4, 0)
                  /       \
        (2, 0, 2, 0)         (3, 3, 4, 0)
       /       \     /   \
     (4, 0, 1, 0)   (5, 2, 2, 0)  (6, 3, 3, 0) (7, 4, 4, 0)
        /          \
    (8, 0, 0, 0)  (9, 1, 1, 0)
    
Each nodes denotes (node_number, start, end, value), where (start, end) is (0, N-1) at the start

Now, after 1st query that is [1, 1, 2] tree will look like:

          (1, 0, 4, 0)
                  /       \
        (2, 0, 2, 0)         (3, 3, 4, 0)
       /       \     /   \
     (4, 0, 1, 0)   (5, 2, 2, 1)  (6, 3, 3, 0) (7, 4, 4, 0)
        /          \
    (8, 0, 0, 0)  (9, 1, 1, 1)
    
2nd query is [2, 1] so we will traverse the tree from root to the node which has (start, end) equivalent to (1, 1) and calculate the total sum of value. So root to leaf path for (1, 1) from above tree is 1 -> 2 -> 4 -> 9 and sum of its values are: 0 + 0 + 0 + 1 = 1 and for arr[1] = 420, we know digitsum list of 420 that is: [420, 6] so the 1st index value is 6 which we will print.

3rd query is [2, 2] so the path from root to leaf having (start, end) = (2, 2) is: 1 -> 2-> 5 and sum of its values are: 0 + 0 + 1 = 1 and for arr[2] = 69, we know digitsum list of 69 that is: [69, 15, 6] and 1st index of the list is 15 which we will print.

Similarly, for the 4th query which is [2, 3] the root to leaf path will be: 1 -> 3 -> 6 and sum of values are: 0 + 0 + 0 = 0 and for arr[3] = 1434, list of digitsum is:
[1434, 12, 3] in which the 0th index is 1434 which we will print.

For 5th query which is [1, 1, 4] which means we have to increment the values of nodes by 1 which lies under (1, 4) so the tree becomes:

          (1, 0, 4, 0)
                  /       \
        (2, 0, 2, 0)         (3, 3, 4, 1)
       /       \     /   \
     (4, 0, 1, 0)   (5, 2, 2, 2)  (6, 3, 3, 0) (7, 4, 4, 0)
        /          \
    (8, 0, 0, 0)  (9, 1, 1, 2)

Now for the 6th query which is [2, 0] the path will be: 1 -> 2 -> 4 -> 8 and the sum of the values are: 0 + 0 + 0 + 0 = 0, and for arr[0] = 1, the digitsum list is: [1], so for 0th index we will print 1.

For the 7th query which is [2, 2] the path will be: 1 -> 2 -> 5 and the sum of the values are: 0 + 0 + 2 = 2, and for arr[2] = 69, the digitsum list is: [69, 15, 6], so for the 2nd index we will print 6.

For the 8th query which is [2, 4], the path will be: 1 -> 3 -> 4 and the sum of the values are: 0 + 1 + 0 = 1, and for the arr[4] = 2023, the digitsum list id: [2023, 7], so for the 1st index we will print 7.

Steps to solve the problem:

  • Firstly, make a Hash Map that contains the list of the digit sum of each element as the value and as for the key we will store the indices. So for the above test case, Hash Map will contain: [[0 -> [1]], [1 -> [420, 6]], [2 -> [69, 15, 6]], [3 -> [1434, 12, 3]], [4 -> [2023, 7]]].
  • After that, make a segment tree in which initially every node contains the value 0.
  • Now, we will traverse the query list and if we got the first integer as 1 then we will apply 1st operation or if we get the first integer as 2 then we will apply 2nd operation.
  • For the 1st operation, we will update the segment tree and increment the nodes by which (start, end) lies between the range given in the query.
  • For the 2nd operation, we will traverse from the root of the segment tree to the leaf whose (start, end) is equal to the given index in the query, and while traversing we will keep a variable idx updated with the sum of the values of the nodes in the root to leaf path. 
  • After traversing, we will fetch the digit sum list from the Hash Map for that query index and check if idx is less than the size of the list we will print that particular index value otherwise we will print the last value of the list.

Below is the implementation of the above approach:


Output
6 15 1434 1 6 7 

Time Complexity: O(N) + O(Q * log(4*N))
Auxiliary Space: O(4 * N)

Comment