Showing my approach and asking yours.

Revision en2, by kaaleen_bhaiya, 2020-07-29 06:32:12

Hi all, recently in an interview i have the following question.

you are given a tree rooted at 1, with N vertices .Each vertex has a value associated with it. The value of the ith vertex is ai. consider fi as the aith beatutiful number.A beautiful number is a positive number whose sum of the square digits is less than or equal to X.

You have to process M queries where each query can be.

1. 1 i y : update ai with y

2. 2 i : output the sum of fi for all the nodes of sub-tree of node i(including node i)

output your ans modulo 10^9+7

Input format 1. N,M,X where N is number of vertices,M no of queries, X is a constant 2. Next N-1 lines will be two vertices which are connected to each other. 3. then you will have M queries.

Constraints 1<=N,M,i,y<=10^5 1<X<10^10

My Approach 1.Created an adjacency list to contain the graph structure of the tree. O(edges)

  1. From that adjacency list created a tree structure. O(n)

  2. if the query is of type 1, replaced that value of node. O(1)

  3. else go to that node, and find maximum among its children& itself. O(n)

  4. Find the starting max beautiful numbers. O(max)

  5. compute the desired sum. O(n)

total complexity O(n+max).

what will be your approach?How i can improve mine?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English kaaleen_bhaiya 2020-07-29 06:32:12 4 Tiny change: 'lexity O(n).\n\nwhat' -> 'lexity O(n+max).\n\nwhat'
en1 English kaaleen_bhaiya 2020-07-29 06:30:28 1364 Initial revision (published)