ΘρϵηΠατπ

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 n2, a repeated element x in A is an element such that Ai=Aj=x, where ij.
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 i0 let gi+1=A[gi]
  • The graph has edges: (gi,gi+1)
We also define the sequnce  𝐠 :=(g0,g1,g2,) as its movement sequence.

The index array graph can be constructed by extending the behavior of 𝚜𝚕𝚘𝚠𝟷 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 j0 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.
From the definition of the index array graph, consider it's movement sequence  𝐠 :=(g0,g1,g2,) and it's set define as X:={gi:i0}, by definition of A the movement sequence contains at most n unique numbers and so we see that |X|n, and thus it is finite, this means that there is some duplicated element in the movement sequence, suppose there is not, then each element is unique, and so X would be infinite, which is a contradiction, thus there is some duplicated element in the movement sequence, and so a meet value exists.
All Index Array Graphs have A Cycle Continuing Forever
As per title.
Since we know that all inputs arrays have a meet value then it's movement sequence is of the form g0,gi,gi+1gi+k,gi,gi+1,gi+k,, where gi is the meet value, and there are k other elements encountered before we get to the meet value. It must continue forever because after gi is encountered for the second element, then the next element is defined as A[gi] which is the same as the definition of gi+1 and so on until gi is reached again, therefore it continues cycling forever.
Termination of the First While Loop
The first while loop terminates and 𝚜𝚕𝚘𝚠𝟷=𝚏𝚊𝚜𝚝, 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

Let 𝚜𝚕𝚘𝚠𝟷k be the value of 𝚜𝚕𝚘𝚠𝟷 at the end of the k-th iteration of the while loop. Observe that gk=𝚜𝚕𝚘𝚠𝟷t, this holds true for k=0 as well where we have g0=0=𝚜𝚕𝚘𝚠𝟷0, note that the equality holds true as each iteration of the while loop directly pertains the recursive definition of gi+1, moreover note that 𝚏𝚊𝚜𝚝k=g2k which can be verified in a similar manner. Note that this means that 𝚏𝚊𝚜𝚝's movement sequence is a subsequence of 𝚜𝚕𝚘𝚠𝟷's denote this subsequence  𝐬 :=(s0,s1,s2,) where si=g2i then we know that 𝚏𝚊𝚜𝚝k=sk.

Let G be the index array graph of A, we know that it has a cycle that it gets stuck in forever, and suppose that the elements of the cycle are given by C=(c0,c1,,ck), where c0 is the meet value.

Consider the smallest index x where gx and sx are both in C for the first time. Since the start of the cycle is the meet value, then we know that gx=c0 whereas sx=ci where i0

Since C is a cycle of length k+1 and 𝚏𝚊𝚜𝚝 traverses C at twice the speed of 𝚜𝚕𝚘𝚠𝟷, (which is to say that gx+y=c(0+y) % k+1 and sx+y=c(y+2y) % k+1) in tandem with the fact that all elements in the cycle are distinct and thus we know that ca=cb if and only if a % (k+1)=b % (k+1), we conclude that gx+y=sx+y when yi+2y(modk+1), where y is the y-th iteration after 𝚜𝚕𝚘𝚠𝟷 and 𝚏𝚊𝚜𝚝 are both in C for the first time.

Rearranging shows yi(modk+1), if i=0, then this is satisfied by y=0 which means that when both 𝚜𝚕𝚘𝚠𝟷=c0=𝚏𝚊𝚜𝚝 when they enter the cycle for the first time. On the other hand if i>0 then y=k+1i satisfies the equation. Note that there are infinitely other non-negative solutions, but these two solutions are the smallest ones, thus the while loop will encounter these first before any larger ones and terminate, thus y{0,,k}.

Termination of the Seond while loop and the Value of 𝚜𝚕𝚘𝚠𝟷 is the Meet Value
The second while loop terminates, and after termination, 𝚜𝚕𝚘𝚠𝟷=gx, where gx is the meet value of the input array A.
Assume the first while loop terminates after t iterations.

Let G be the index array graph of A. By Corollary: All Index Array Graphs have A Cycle Continuing Forever, G has a cycle C=(c0,c1,,ck). Without a loss of generality, assume c0=gx. Let 𝐠=(g0,g1,g2,) be the movement sequence of G. Now, let y0 be the smallest number such that gx+y=𝚜𝚕𝚘𝚠𝟷t at iteration t (i.e., termination) of the first while loop. Finally, let z0 be the smallest number such that gx+y+z=c0. It is worth noting that y+z=k+1, thus y+z is the length of C.

We know that the first while loop terminates at iteration t=x+y . Since 𝚏𝚊𝚜𝚝t=𝚜𝚕𝚘𝚠𝟷2t at iteration t, we know 𝚏𝚊𝚜𝚝x+y=𝚜𝚕𝚘𝚠𝟷2(x+y)=g2(x+y). Therefore, we can say that upon termination of the first while loop, 𝚏𝚊𝚜𝚝 has travelled 2(x+y) nodes into 𝐠.

For an alternative but equivalent statement of this fact, assume 𝚏𝚊𝚜𝚝 completes n full cycles around C by iteration t. Then, 𝚏𝚊𝚜𝚝t=gx+n(k+1)+y=gx+n(y+z)+y. Hence, we can also say that upon termination of the first while loop, 𝚏𝚊𝚜𝚝 has travelled x+n(y+z)+y nodes into 𝐠.

These two equivalent statements brings us to the following result. 2(x+y)=x+n(y+z)+y2x+2y=x+(n+1)y+nzx=(n1)y+nzx=(n1)(y+z)+zx=(n1)(k+1)+z Therefore, travelling x nodes into 𝐠 is equivalent to completing n1 complete cycles around C and travelling another z nodes.

Now consider the second while loop. On iteration 0, 𝚜𝚕𝚘𝚠𝟷=gx+y and 𝚜𝚕𝚘𝚠𝟸=g0. Now consider iteration x. On iteration x, 𝚜𝚕𝚘𝚠𝟷=gx+y+x. gx+y+x=gx+y+(n1)(k+1)+z=gx+(y+z)+(n1)(k+1)=gx+(k+1)+(n1)(k+1)=gx+n(k+1) Given gx is in C, gx+y+x is just n complete cycles around C from gx. Therefore, gx+y+x=gx. On iteration x, we also trivially know 𝚜𝚕𝚘𝚠𝟸=gx. Hence, 𝚜𝚕𝚘𝚠𝟷=𝚜𝚕𝚘𝚠𝟸 so the second while loop terminates, and upon termination, 𝚜𝚕𝚘𝚠𝟷=gx.
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 mA be the meet value, then m=d

By definition of the meet value, it is encountered twice in the movement sequence  𝐠 :=(g0,g1,g2,) on it's first encounter, we have some index i such that gi=m by the definition of the movement sequence this means that A[gi1]=m so that gi1 is the index of m in A

On the second encounter of m in the movement sequence we have some j where (j>i) such that gj=m so also that A[gj1]=m. Now note that with respect to the movement sequence, we have the following subsequence gi1,m,,gj1,m, if gi1=gj1 then we get contradiction because by the definition of the meet value gj=m it's assumed that j was the smallest value such that (g0,,gm) has a duplicated value, but under the assumption that gi1=gj1 then j1 would be a smaller value such that (g0,,gj1) has a repeated value, therefore we must have that gi1gj1.

Recall that A[gi1]=A[gj1]=m therefore m is duplicated in A . Since A contains exactly one duplicated element, then we must conclude that d=m.

𝚏𝚒𝚗𝚍_𝚍𝚞𝚙𝚕𝚒𝚌𝚊𝚝𝚎 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.
We know that the first while loop terminates. We know that the second while loop terminates (therefore the entire program terminates) and the value of 𝚜𝚕𝚘𝚠𝟷 is the meet value. We also know that the meet value is the duplicated value therefore since the algorithm returns 𝚜𝚕𝚘𝚠𝟷 then it returns the duplicated value.