> For the complete documentation index, see [llms.txt](https://welchj.gitbook.io/cp-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://welchj.gitbook.io/cp-notes/foundementals/wormhole-traversal.md).

# Wormhole Traversal

I named this category :)

I've seen a few specific problems from USACO as well as Leetcode that uses this idea.

## Concepts

***Whenever the value of an array element is used as the index for accessing another element, it is a wormhole...***

If we have a 1D array of size `n`, and every element has a **unique** value between `[0,n-1]` (so guarantees no index out of bounds) or similar (sometimes problem context varies), we should be sensitive that a Wormhole Traversal may be used on this array.

Essentially we treat the value of each element of this array as the index of the next element to go to, and turn the array traversal into a linkedlist traversal. This linked list is guaranteed to contain cycles (again, may vary). Any further cycle detections/manipulations can be potentially connected to [Floyd's Cycle Detection](/cp-notes/advanced/graph-theory/floyds-cycle-detection.md)

## Notes

* This is a very insightful choice especially when numbers are unique in an array, which established the constraints that the `in-degree` and `out-degree` of every element is strictly 1.

## Problems

{% embed url="<https://leetcode.com/problems/find-the-duplicate-number/description/>" %}

{% embed url="<https://leetcode.com/problems/find-all-duplicates-in-an-array/description/>" %}
