Description

You are given an integer array nums of length n where nums is a permutation of [0, n-1]. Build a set starting with A[i], then A[A[i]], then A[A[A[i]]], etc. Return the longest length of a set.

Examples

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

Set {5,6,2,0} has length 4.

Input:nums = [1,0,2,4,3]
Output:3
Explanation:

The longest nesting cycle has length 3, so the answer is 3.

Input:nums = [2,1,3,0]
Output:2
Explanation:

The longest nesting cycle has length 2, so the answer is 2.

Constraints

  • 1 ≤ nums.length ≤ 10^5
  • 0 ≤ nums[i] < nums.length

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!