XOR Query
You are given a tree(root 0) with N vertex having N - 1 edges. You are also given an array ‘QUERY’ of size ‘Q’, where each query consists of an integer that denotes a node. You have to print the xor of all the values of nodes in the sub-tree of the given node.
Note:
Here XOR denotes the bitwise operator (^).
Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and 'Q', denoting the number of nodes and the total queries operation to be performed.
The next ‘N’-1 lines of each test case contain two integers representing an edge between the given indices.
The next line of each test case contains ‘Q’ space-separated integers denoting the nodes of the tree on which query has to be performed.
Output Format:
For each test case, print ‘Q’ space-separated integers denoting the result of the queries.
Print the output of each test case in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
0 <= N, Q <= 10^5
0 <= QUERY[i] < N
Time Limit: 1 sec
AnswerBot
1y
This question is about finding the XOR of all values of nodes in the sub-tree of a given node in a tree.
Read the input values for the number of test cases, number of nodes, and number of queries.
Const...read more
CodingNinjas
author
2y
Brute Force
The basic idea is to traverse the subtree for each given node in the query. We traverse the nodes using the DFS and compute the XOR value of the subtree while traversing the tree.
Here is t...read more
CodingNinjas
author
2y
Optimised Approach
The basic idea is to precompute the XOR of all the nodes of the sub-tree by traversing the tree. We start by storing the XOR values from the child node and then using the value of th...read more
Add answer anonymously...
Top Protium Finance Software Developer interview questions & answers
Popular interview questions of Software Developer
>
Protium Finance Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app