Consider the following problem: Given an array of integers nums containing integers where each integer is in the range 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
- Set and for any let
- The graph has edges:
The index array graph can be constructed by extending the behavior of in the first while loop.
Let be the value of at the end of the -th iteration of the while loop. Observe that , this holds true for as well where we have , note that the equality holds true as each iteration of the while loop directly pertains the recursive definition of , moreover note that which can be verified in a similar manner. Note that this means that 's movement sequence is a subsequence of 's denote this subsequence where then we know that .
Let be the index array graph of , we know that it has a cycle that it gets stuck in forever, and suppose that the elements of the cycle are given by , where is the meet value.
Consider the smallest index where and are both in for the first time. Since the start of the cycle is the meet value, then we know that whereas where
Since is a cycle of length and traverses at twice the speed of , (which is to say that and ) in tandem with the fact that all elements in the cycle are distinct and thus we know that if and only if , we conclude that when , where is the -th iteration after and are both in for the first time.
Rearranging shows , if , then this is satisfied by which means that when both when they enter the cycle for the first time. On the other hand if then 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 .
Let be the index array graph of . By Corollary: All Index Array Graphs have A Cycle Continuing Forever, has a cycle . Without a loss of generality, assume . Let be the movement sequence of . Now, let be the smallest number such that at iteration (i.e., termination) of the first while loop. Finally, let be the smallest number such that . It is worth noting that , thus is the length of .
We know that the first while loop terminates at iteration . Since at iteration , we know . Therefore, we can say that upon termination of the first while loop, has travelled nodes into .
For an alternative but equivalent statement of this fact, assume completes full cycles around by iteration . Then, . Hence, we can also say that upon termination of the first while loop, has travelled nodes into .
These two equivalent statements brings us to the following result. Therefore, travelling nodes into is equivalent to completing complete cycles around and travelling another nodes.
Now consider the second while loop. On iteration 0, and . Now consider iteration . On iteration , . Given is in , is just complete cycles around from . Therefore, . On iteration , we also trivially know . Hence, so the second while loop terminates, and upon termination, .
By definition of the meet value, it is encountered twice in the movement sequence on it's first encounter, we have some index such that by the definition of the movement sequence this means that so that is the index of in
On the second encounter of in the movement sequence we have some where such that so also that . Now note that with respect to the movement sequence, we have the following subsequence , if then we get contradiction because by the definition of the meet value it's assumed that was the smallest value such that has a duplicated value, but under the assumption that then would be a smaller value such that has a repeated value, therefore we must have that .
Recall that therefore is duplicated in . Since contains exactly one duplicated element, then we must conclude that .