New update is available. Click here to update.

Maximum Distinct Blocks

Posted: 13 Mar, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

You are given 'N' blocks which are numbered from 0 to 'N' - 1 and are arranged in a straight line. Each block has an integer label on it which denotes the index of the next block that you can go to from that block. The labels on each block are given in the array 'ARR'. Your task is to find out the maximum number of distinct blocks that you can reach by starting from any arbitrary block and moving through all the blocks that you are allowed to move from that block and all other subsequent blocks.

For example:

Consider the array ARR = { 0, 2, 1 } having 3 elements. 
Starting at Block 0, you can move to only Block 0 and you cannot move to any other blocks. 
Starting at Block 1, you can move between Block 1 and Block 2. 
Starting at Block 2, you can move between Block 2 and Block 1.
Hence, the maximum number of distinct that you can visit is 2 in this case. 
Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.

The first line of each test case contains an integer, 'N,’ denoting the number of elements in the array 'ARR'

The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
Output Format:
For each test case, return the maximum number of distinct blocks that you can reach by starting from an arbitrary block. 
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^5 
0 <= ARR[i] < N

All elements of the array ARR are pairwise distinct.

Time limit: 1 sec