Tap here to switch tabs
Problem
Submissions

Problem: Array Nesting

med
30 min
Try to solve the Array Nesting problem.

Statement

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

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

  • The first element in s[k]s[k] starts with the selection of the element nums[k] of index kk.

  • The next element in s[k]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]s[k].

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

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] << nums.length

  • All the values of nums are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Array Nesting

med
30 min
Try to solve the Array Nesting problem.

Statement

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

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

  • The first element in s[k]s[k] starts with the selection of the element nums[k] of index kk.

  • The next element in s[k]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]s[k].

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

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] << nums.length

  • All the values of nums are unique.