<p>You are given an integer array <code>nums</code> of length <code>n</code> where <code>nums</code> is a permutation of the numbers in the range <code>[0, n - 1]</code>.</p>

<p>You should build a set <code>s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... }</code> subjected to the following rule:</p>

<ul>
	<li>The first element in <code>s[k]</code> starts with the selection of the element <code>nums[k]</code> of <code>index = k</code>.</li>
	<li>The next element in <code>s[k]</code> should be <code>nums[nums[k]]</code>, and then <code>nums[nums[nums[k]]]</code>, and so on.</li>
	<li>We stop adding right before a duplicate element occurs in <code>s[k]</code>.</li>
</ul>

<p>Return <em>the longest length of a set</em> <code>s[k]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,4,0,3,1,6,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2.
One of the longest sets s[k]:
s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,2]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt; nums.length</code></li>
	<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
</ul>

class Solution:
    def arrayNesting(self, nums: List[int]) -> int:
        index_to_distance = [-1] * len(nums)  # index to num starting from that index
        for i, num in enumerate(nums):
            if index_to_distance[i] != -1:
                continue

            # explore starting with i, and then stop when there's a cycle
            # stack of indices that's visited starting from i
            visited_indices = set()
            visited_path = []
            to_visit = i
            distance = 0
            while to_visit is not None:
                # visit index j in nums
                j = to_visit
                to_visit = None
                visited_path.append(j)
                visited_indices.add(j)

                # check if this index has already been visited previously
                if index_to_distance[j] != -1:
                    distance += index_to_distance[j]
                    break

                # count the distance to this index
                distance += 1

                # find the next index
                k = nums[j]
                # check if next index has already been visited in this path (cycle)
                if k in visited_indices:
                    break
                to_visit = k

            # update the distances for the visited indices
            for j_index in range(len(visited_path) - 1, - 1, -1):
                j = visited_path[j_index]
                index_to_distance[j] = distance - j_index

            # ex.
            # distance[1] = -1
            # distance[2] = -1
            # distance[3] = 2
            # distance[4] = 1
            # path:     1 -> 2 -> [3 -> 4] = 2 where [3 -> 4] is a previously visited path
            # distance: 1    2     +2      = 4
            # visited = [1,2,3]
            # j_index = 2, distance[j=3] = 4 - 2 = 2
            # j_index = 1, distance[j=2] = 4 - 1 = 3
            # j_index = 0, distance[j=1] = 4 - 0 = 4

        return max(index_to_distance)


32791