Firing employees
##### Submissions: 2590   Accuracy: 22.65%   Difficulty: Medium   Marks: 4

Mr. Alfred is the founder of FooLand Constructions. He always maintains a ‘Black list’ of potential employees which can be fired at any moment without any prior notice.

This company has N employees (including Mr. Alfred), and each employee is assigned a rank (1 <= rank <= N) at the time of joining the company. Any person can have any rank from 1 to N. No two persons have the same rank. The company has a hierarchically organized management. Each employee has one immediate senior but can have any number of seniors. If a person A is the senior of B and B is senior of C, this implies that A is also a senior of C. Note that Mr. Alfred has no senior to him.

Mr. Alfred has a strange and unfair way of evaluating an employee’s performance. Those employees the sum of whose rank and the number of his/her seniors is a prime number is put up on the ‘Black list’.

You are required to find out the number of ‘Black listed’ employees.

Note: The list won’t contain Mr. Alfred’s name as he is the founder of the company and the list is managed by him!

Input:

The first line of input consists of a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line contains a single integer N as described in the problem statement. The next line contains N integers. The ith integer (1 <= i <= N) represents the rank of the immediate senior of the ith employee (i.e. the employee with rank = i). If the ith integer is 0, it represents that employee with rank = i is Mr. Alfred as he has no superior.

Output:

For each test case, print in a new line the number of ‘Black listed’ employees.

Constraints:

1 <= T <= 10^3

1 <= N <= 10^5

Example:

Input:

2

4

0 1 1 2

3

2 3 0

Output:

1

2

Explanation:

For 1st case:

The employee with rank 1 is Mr. Alfred.

The employee with rank 2 and rank 3 have Mr. Alfred as their senior.

2 + 1 = 3 is a prime and 3 + 1 = 4 is not a prime.

The employee with rank 4 has Employee 2 as its immediate senior. Hence, number of seniors of Employee 4 = 2 (i.e. employee 2 and Mr. Alfred)

4 + 2 = 6 is not a prime.

Hence, the result is 1.

For 2nd case:

Employee 3 is Mr. Alfred.

Employee 2 is Mr. Alfred’s immediate subordinate. His level is 1.

2 + 1 = 3 is a prime.

Employee 1 is Employee 2’s immediate subordinate. His level is 2.

1 + 2 = 3 is a prime.

Hence, the result is 2.

#### ** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sajal Agrawal
Author: sajalagrawal

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.