Skip to content

Practice Array Nesting

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#565
LeetCode Problem

Array Nesting

Problem Statement

nums is a permutation of the integers from 0 through nums.length - 1. For a start k, form a sequence beginning with nums[k], then nums[nums[k]], and continue by using the previous value as the next index. Stop before adding a value already present. Return the maximum sequence length over all starts.

Source: Array Nesting on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [5,4,0,3,1,6,2]
Output
4

# Example 2

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.

Related LeetCode Problems

Practice more problems similar to Array Nesting.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.