Question: HandsOn 2 : Nodes Reordering You are given a tree which consists of N nodes and an array A where A [ U ] represents

HandsOn
2: Nodes Reordering
You are given a tree which consists of N nodes and an array A where A[U] represents the value of node U.
You are also given an array P where the parent of node U is given by .
We say that the path from U to V is considered good if for each pair of adjacent nodes i,j, the values written on these nodes satisfy the following condition:
A[i]A[j] is smaller than both A[i] and A[j](where ^ is the bitwise xor operator).
Find the maximum length of a good path that you can get.
Note:
It is given that P[1]=0.
Input Format
The first line contains an integer, N, denoting the number of elements in P.
Each line i of the N subsequent lines (where 1=NN105
0P[i]N.1A[i]1050i0i
Sample Test Cases
Case 1
Input:
Input:
5
0
1
2
3
4
1
1
1
1
1
Output:
5
Explanation:
The good path consists of nodes 1,2,3,4,5. They satisfy the condition as the pair of value of nodes present in good path has a XOR equal to 0, which is smaller than the values of the nodes.
Hence, the answer for this case is equal to 5.
Case 2
Input:
5
0
1
2
3
4
2
2
4
6
2
Output:
3
Explanation:
Given ,2,4,6,2.
The good pafs consists of nodes 3,4,5. They satisfy the condition as the pair of value of nodes present in good path has a XOR which is maller than the values of the nodes.
Hence, the answer for this case is equal to 3
Case 3
Input:
4
o
1
1
1
1
1
1
1000
Output:
3
Explanation:
]
[1,1,1000
The good path consists of nodes 1,2,3. They satisfy the condition as the pair of value of nodes present in good path has a XOR of which is smaller than the values of the nodes.
complete the function .public static int get_ans(int N, List P, List A)
 HandsOn 2: Nodes Reordering You are given a tree which consists

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 Databases Questions!