šŸ—ļø Ī˜ĻĻµĪ·Ī Ī±Ļ„Ļ€šŸš§ (under construction)

Consider the following problem: Given an array of integers nums containing n+1 integers where each integer is in the range [1,ā€¦,n] inclusive, where there is only one repeated number in nums return this repeated number.

We propose that the following algorithm does this:

# Note this is in Python
def find_duplicate(nums: List[int]) -> int:
    # loop 1
    slow1 = 0
    fast = 0
    while True:
        slow1 = nums[slow1]
        fast = nums[nums[fast]]
        if slow1 == fast:
            break

    # loop 2
    slow2 = 0
    while slow1 != slow2:
        slow1 = nums[slow1]
        slow2 = nums[slow2]
    return slow1
        

Repeated Element in Tuple
Given a n-tuple A, where nā‰„2, a repeated element x in A is an element such that Ai=Aj=x, where iā‰ j.
Index Array
An index array is an array whose values are valid indices to itself
Index Array Graph
Given an index array A then it's induced graph G is a directed graph whose vertices are the elements of A, it's edges are obtained as follows,
  • Set g0=0 and for any iāˆˆN0 let gi+1=A[gi]
  • The graph has edges: (gi,gi+1)
We also define the sequnce Ā gĀ :=(g0,g1,g2,ā€¦) as its movement sequence.

The index array graph can be constructed by extending the behavior of slow1 in the first while loop.

Meet Value
Given the index array graph for an index array A, we define the meet value as gj where jāˆˆN0 is the smallest index such that (g0,g1,g2,ā€¦gj) has a repeated element.
All Input Arrays have a Meet Value
Suppose that A is an array of n+1 integers such that aiāˆˆ[1,ā€¦,n], such that there is only one repeated number, then A has a meet value.
All Index Array Graphs have A Cycle Continuing Forever
As per title.
Termination of the First While Loop
The first while loop terminates and slow1=fast, moreover it takes x+y iterations to terminate where x is the index of the meet value, and yāˆˆ{0,ā€¦k} where k+1 is the length of the cycle that is guaranteed to exist
Termination of the Seond while loop and the Value of slow1 is the Meet Value
The second while loop terminates, and after termination, slow1=gx, where gx is the meet value of the input array A.
The Meet Value is the Duplicated Value
Let A an an array which satisfies the specification of the algorithm where d is its duplicated element and māˆˆA be the meet value, then m=d
find_duplicate is Correct
Given an (n+1)-tuple consisting of elements in the range [1,n] with exactly one repeated element, the algorithm above returns that element.