Company: BNY Codedivas 2026
Difficulty: medium
There is a tree with N vertices. The vertices are numbered from 1 to N, but they are assigned in a random order. A path between vertex X and Y (X < Y) is called an ideal path , if the values of all the other vertices in the entire path fall within the integer range of X and Y. An array A is given as [A 1 , A 2 , ..., A N ], which contains the mapping for each element. For each valid i (1 <= i <= N), if A i != 0, we have an edge between vertex i and vertex A i . There can be only one A i = 0, which points to the root of the tree. If a number is present at two indices, then that number is the parent of both indices. For example , Consider N = 7 and A = {0,5,5,1,4,1,4} For each i, the mapping in array A is as follows: i 1 2 3 4 5 6 7 A[i] 0 5 5 1 4 1 4 Here A[1] = 0, indicating that 1 is the root. A[2] and A[3] = 5, meaning 5 is the parent node for vertices 2 and 3. A[4] and A[6] = 1, meaning 1 is the parent node for vertices 4 and 6. A[5] and A[7] = 4, meaning 4 is the parent no