Locate Duplicate Integer with Strict Space Limit
Linked ListsAuto-graded · 19 test casesLast updated
Find the single repeated value in an array of size n + 1 containing integers from 1 to n without altering the array or using extra space.
You are given a list nums containing $n + 1$ integers where every integer is between $1$ and $n$ inclusive. By the pigeonhole principle, at least one number in nums must appear more than once. The list contains exactly one value that repeats, though it may appear two, three, or more times.
Your task is to identify and return this repeated integer.
You must solve the problem under strict constraints:
- You cannot modify the input array in any way (no sorting, swapping, or element sign flipping).
- You must use only $O(1)$ constant extra space (no hash sets, hash maps, frequency arrays, or copies of the data).
Constraints
- $1 \le n \le 10^5$
len(nums)$= n + 1$- $1 \le \text{nums}[i] \le n$ for all valid $i$
- Exactly one number in
numsis duplicated (occurring $\ge 2$ times).
Examples
Example 1
Input: find_repeated([4, 1, 3, 2, 4])
Output: 4
Example 2
Input: find_repeated([1, 1])
Output: 1
Example 3
Input: find_repeated([5, 2, 5, 1, 5, 3])
Output: 5
Hints
Hint 1
Consider treating the array as a directed graph or linked list where each index $i$ points to the index $\text{nums}[i]$.
Hint 2
Since values range from $1$ to $n$, every value is a valid index inside the array. Index $0$ is never pointed to by any value.
Hint 3
Using Floyd's cycle detection algorithm (tortoise and hare), locate the entrance to the cycle. The entrance node corresponds to the duplicate value.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can view the array as a implicit functional graph or single linked list where the index $i$ points to the next node located at index nums[i].
- Cycle Existence: Since there are $n+1$ positions and all values are in the range $[1, n]$, following pointers starting from index $0$ will inevitably revisit an index already seen, forming a cycle.
- Cycle Entrance as the Duplicate: Because index $0$ is never pointed to by any value (values are $\ge 1$), index $0$ lies outside the cycle on a tail leading into it. The cycle entrance node has at least two incoming edges: one from the tail and at least one from within the cycle. Nodes with multiple incoming edges correspond to indices that share the exact same value. Thus, the index where the cycle starts is the duplicated number.
We can apply Floyd's Tortoise and Hare algorithm in two phases:
- Phase 1 (Detect Intersection): Advance a
slowpointer by 1 step (slow = nums[slow]) and afastpointer by 2 steps (fast = nums[nums[fast]]). They are guaranteed to collide inside the cycle. - Phase 2 (Find Cycle Entrance): Reset the
slowpointer tonums[0](or index $0$) and keepfastat the collision point. Advance both pointers 1 step at a time (slow = nums[slow],fast = nums[fast]). The point where they meet is the entrance to the cycle, which is our duplicate value.
Complexity Analysis
- Time Complexity: $\mathcal{O}(n)$. In Phase 1, the fast pointer traverses at most $2n$ steps before meeting slow. In Phase 2, both pointers walk at most $n$ steps.
- Space Complexity: $\mathcal{O}(1)$. Only two pointer variables are used, meeting all space constraints without modifying the input array.
Python Reference Solution
def find_repeated(nums: list[int]) -> int:
# Phase 1: Locate the meeting point inside the cycle
slow = fast = nums[0]
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
# Phase 2: Locate the entrance to the cycle
slow = nums[0]
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow