/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class ArrayNesting
{
public int findSetS(int[] nums) {
boolean[] visited = new boolean[nums.length];
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
int start = nums[i], count = 0;
do {
start = nums[start];
count++;
visited[start] = true;
}
while (start != nums[i]);
res
= Math.
max(res, count
); }
}
return res;
}
public static void main
(String[] args
) {
ArrayNesting arrayNesting = new ArrayNesting();
int[] arr = new int[]{5, 4, 0, 3, 1, 6, 2};
System.
out.
println(arrayNesting.
findSetS(arr
)); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgQXJyYXlOZXN0aW5nCnsKCXB1YmxpYyBpbnQgZmluZFNldFMoaW50W10gbnVtcykgewogICAgYm9vbGVhbltdIHZpc2l0ZWQgPSBuZXcgYm9vbGVhbltudW1zLmxlbmd0aF07CiAgICBpbnQgcmVzID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbnVtcy5sZW5ndGg7IGkrKykgewogICAgICBpZiAoIXZpc2l0ZWRbaV0pIHsKICAgICAgICBpbnQgc3RhcnQgPSBudW1zW2ldLCBjb3VudCA9IDA7CiAgICAgICAgZG8gewogICAgICAgICAgc3RhcnQgPSBudW1zW3N0YXJ0XTsKICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICB2aXNpdGVkW3N0YXJ0XSA9IHRydWU7CiAgICAgICAgfQogICAgICAgIHdoaWxlIChzdGFydCAhPSBudW1zW2ldKTsKICAgICAgICByZXMgPSBNYXRo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