> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Array Nesting Python Solution with Tests

> Tested Python solution for LeetCode 565 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 565, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Depth-First Search](/catalog/topics/depth-first-search). [View on LeetCode](https://leetcode.com/problems/array-nesting/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 565   # by problem number
lcpy gen -s array_nesting   # by problem name
```

## Problem

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

You should build a set `s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... }` subjected to the following rule:

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

Return the longest length of a set `s[k]`.

### Examples

```
Input: nums = [5,4,0,3,1,6,2]
Output: 4
Explanation: 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}
```

```
Input: nums = [0,1,2]
Output: 1
```

### Constraints

* 1 \<= nums.length \<= 10^5
* 0 \<= nums\[i] \< nums.length
* All the values of `nums` are unique.

**Follow-up:** Could you solve it using constant extra space complexity?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/array_nesting/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/array_nesting/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) - each index is visited exactly once across all cycles
    # Space: O(1) - marks visited in place, no extra set
    def array_nesting(self, nums: list[int]) -> int:
        best = 0
        for i in range(len(nums)):
            if nums[i] < 0:
                continue
            count = 0
            j = i
            while nums[j] >= 0:
                nxt = nums[j]
                nums[j] = -1
                j = nxt
                count += 1
            best = max(best, count)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) - each index is visited exactly once across all cycles | O(1) - marks visited in place, no extra set |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.