Problem

Submissions

0

Difficulty: MEDIUM

Avg. time to solve

40 min

Success Rate

60%

Problem Statement

```
The first line of input contains a single integer ‘T’, denoting the number of test cases.
The second line of each test case contains a positive integer ‘N’, denoting the number of nodes.
The third line of each test case contains the parent array.
The fourth line of each test case contains a node value and k value.
```

```
The first and only line of each test case contains an integer denoting the kth ancestor.
```

```
You are not required to print the expected output; it has already been taken care of. Just implement the function.
```

```
1 <= T <= 10
1 <= k <= N <= 5 * 10 ^ 4
0 <= node < N
0 <= parent[i] < N
Time limit: 1 second
```

```
2
7
-1 0 0 1 1 2 2
3 1
7
-1 0 0 1 1 2 2
5 2
```

```
1
0
```

```
In the first test case, it is visible from the figure that the 1st ancestor of a node with the value of 3 is the parent node of 3. Parent of 3 is 1.So we return 1 as our answer.
In the second test case, the first ancestor of the node with the value 5 is 2. Now to find the 2nd ancestor of this node, we find the 1st ancestor of the parent of this node. Now, 1st ancestor of 2 is 0. So we return 0 as our answer
```

```
2
7
-1 0 0 1 1 2 2
4 2
7
-1 0 0 1 1 2 2
6 1
```

```
0
2
```

Console