Question: Solve this below problem in Java with proper explanation. Method to use is also given at the end of question Question - You are given

Solve this below problem in Java with proper explanation. Method to use is also given at the end of question
Question -
You are given a tree T consisting of N nodes and M edges.
You are given an array A, where each node U in the tree have a value A[U]. You are also given a 2D array E where the ith value denotes that there is an edge between E[i][0] and E[i][1] for all 0< i < N.
You are given Q queries in a 2D array Queries which are of the following two types:
Type 1: (1, U, X)- Set A[U]= X.
Type 2: (2, U, V)- Count the number of nodes that have A[U] mod 3= X mod 3, where X is the index of the current query.
Find the sum of answers to all queries of Type 2. Since the answer can be very large, return it modulo10^9+7.
Constraints
1<= N <=10^5
N -1<= M <= N -1
2<= two <=2
1<= A[i]<=10^9
1<= E[i][j]<= N
1<= Q <=10^5
3<= three <=3
1<= Queries[i][j]<=10^9
Sample Test Cases
Case 1
Input:
N =2
M =1
two =2
A[]={1,3}
E =[[1,2]]
Q =1
three =3
Queries =[[2,1,2]]
Output:
1
Explanation:
Given N =2, M=1, two =2, A =[13], E =[[1,2]], Q =1, three =3, Queries =[[2,1,2]].
For the 1st query the value of 1 mod 3 is to equal 1.
Hence, the sum of queries of type 2isequalto1.
Case 2
Input:
2
1
2
3
3
12
1
3
212
Output:
0
Explanation:
Given N =2, M=1, two =2, A [3,3], E[[1,2]], Q=1, three=3. Queries =[[2,1,2]].
For the 1st query the answer is equal to 0.
Hence, the sum of queries of type 2isequalto0.
Case 3
Input:
2
1
2
1
2
12
2
3
112
212
Output:
2
Explanation:
Given N=2, M=1, two=2, A=[1,2], E=[[1,2]], Q2, three =3, Queries=[[1,1,2],[2,1,2]].
For the 1st query we update value of node 1 to 2.
For the 2nd query, the value of 2%3=2 and the number of node on path (1,2) with value equal to 2 mod 3 is 2.
Hence, the sum of queries of type 2isequalto2.
Method to use -
public static int calc(int N, int M, int two, List A, List> E, int Q, int three, List> Queries){
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!