Company: Impetus_21nov
Difficulty: medium
Number of Nodes Problem Description You are given a tree having N nodes such that the i-th node is indexed as i. The Value of each node is given in array V, where V[i] is the Value of the i-th node. For each index i (1 <= i <= N), find the number of nodes in subtree of the i-th node which have Value less than V[i]/2 rounded down to the nearest integer and call it the result for this i. Find the sum of results for all nodes. Notes: - It is guaranteed that the answer always fits in the integer range. - Parent of nodes is given in array P, where P[i] is the parent of the i-th node. - The root of the tree is node 1 and P[1]=0. - The tree uses 1-based indexing. Examples Example 1: Input: N = 3, P = [0, 1, 2], V = [9, 4, 1] Output: 3 Explanation: - In subtree of 1, node 2 has value 4 which is less than 9/2 (4.5), and node 3 has value 1 which is also less than 9/2. So there will be 2 such nodes for 1. - In subtree of 2, node 3 has value 1 which is less than 4/2 (2), so result will be 1 for 2.