Skip to content

Instantly share code, notes, and snippets.

@iamprayush
Created August 19, 2020 12:17
Show Gist options
  • Save iamprayush/ca4d1d00abbb8410a4801b5188d13f05 to your computer and use it in GitHub Desktop.
Save iamprayush/ca4d1d00abbb8410a4801b5188d13f05 to your computer and use it in GitHub Desktop.
Find the Duplicate Number
class Solution:
def findDuplicate(self, arr: List[int]) -> int:
# Awesome proof here: https://youtu.be/9YTjXqqJEFE?t=431
hare, tortoise = 0, 0
low_speed = False
while True:
if low_speed:
hare = arr[hare]
else:
hare = arr[arr[hare]]
tortoise = arr[tortoise]
if hare == tortoise:
if low_speed:
return hare
hare = 0
low_speed = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment