Array Nesting - Problem

Imagine you're following a treasure map where each location points to another location, and you want to find the longest possible path before returning to a place you've already visited!

You are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n-1]. Think of each index as a location, and each value as directions to the next location.

For any starting index k, you can build a set s[k] by following this path:

  • Start at nums[k]
  • Next, go to nums[nums[k]]
  • Then nums[nums[nums[k]]]
  • Continue until you would revisit a number you've already seen

Goal: Return the maximum length of any such set s[k].

Example: If nums = [5,4,0,3,1,6,2], starting from index 0: we get 5 → 6 → 2 → 0 → 5 (length 4 before repeating).

Input & Output

example_1.py — Basic Case
$ Input: [5,4,0,3,1,6,2]
Output: 4
💡 Note: Starting from index 0: nums[0]=5 → nums[5]=6 → nums[6]=2 → nums[2]=0 → back to nums[0]=5. The cycle 0→5→6→2→0 has length 4, which is the maximum possible.
example_2.py — Simple Case
$ Input: [0,1,2]
Output: 1
💡 Note: Each element points to itself or forms cycles of length 1: 0→0, 1→1, 2→2. Maximum length is 1.
example_3.py — Two Element Cycle
$ Input: [0,2,1]
Output: 2
💡 Note: Starting from index 1: nums[1]=2 → nums[2]=1 → back to nums[1]=2. The cycle 1→2→1 has length 2. Index 0 forms a self-loop with length 1. Maximum is 2.

Visualization

Tap to expand
🗺️ Treasure Map ExplorationLocation 05Location 14Location 20Location 33Location 41Location 56Location 62Step 1Step 2Step 3Step 4 - Back to start!Cycle 2Self-loopTreasure Hunt Results:🏆 Longest Path: 0→5→6→2→0 (Length 4)🥈 Medium Path: 1→4→1 (Length 2)🥉 Short Path: 3→3 (Length 1)
Understanding the Visualization
1
Start Exploration
Begin at any unvisited location and start following the treasure map directions
2
Follow the Trail
Keep following nums[current] to get the next location, marking each as visited
3
Detect Cycle
Stop when you reach a location you've already visited - you've found a cycle!
4
Skip Explored Areas
Move to next unvisited location, skipping areas already explored
5
Find Maximum
The longest treasure hunt path is your answer!
Key Takeaway
🎯 Key Insight: Since it's a permutation, every element belongs to exactly one cycle. Mark elements as visited globally to avoid re-exploring cycles, achieving optimal O(n) time!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each element is visited exactly once across all iterations

n
2n
Linear Growth
Space Complexity
O(1)

Only need a boolean visited array of size n, or can modify input array

n
2n
Linear Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] < nums.length
  • nums is a permutation of numbers [0, 1, ..., n-1]
  • All integers nums[i] are unique
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
42.8K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen