You are given a list nums of n + 1 whole numbers. Every number is between 1 and n. Exactly one value is repeated, and it may appear two or more times. Return that value.
First, why must a repeat exist? There are n + 1 slots, but only n possible values, 1 to n. If every slot held a different value, you would need n + 1 different values. You only have n. So at least two slots share a value. This is called the pigeonhole principle: if you put more pigeons than holes, some hole holds two pigeons.
Now look at the rules. Every obvious method is banned.
A set of seen values uses O(n) memory. Banned.
Sorting, then comparing neighbours changes the list. Banned.
Marking seen values by making nums[x] negative changes the list. Banned.
When a problem forbids every obvious method, the rules are a hint. Ask what is left. Here, almost nothing is, which is why the intended answer is surprising.
Treat the list as a set of signposts. From position i, the signpost says "go to position nums[i]".
That is always allowed. Every value is between 1 and n, and the list has positions 0 to n, so every value is a real position. Each position has exactly one signpost, so each position leads to exactly one next position. That is a linked list, built inside a plain list, with no pointers at all.
Now the key facts.
Start at position 0. No value is 0, so no signpost ever leads into position 0. That makes it a safe place to start, outside any loop.
The walk must enter a cycle. There are only n + 1 positions, so the walk must eventually revisit one.
The repeated value is the entrance to the cycle. Two different positions hold the repeated value, so two different signposts point to the same position. That position is where two paths merge, and in a "one next step" structure, the point where paths merge is the start of the cycle.
nums = [1, 3, 4, 2, 2]position: 0 → 1 → 3 → 2 → 4 → 2 → 4 → ... ↑_______|positions 3 and 4 both hold the value 2, so both point to position 2.the cycle starts at position 2, and 2 is the answer.
Finding the start of a cycle with no extra memory is exactly what 4.9.7 taught. We use the same two phases, with nums[i] in place of node.next.
(1) Both pointers start at position 0, which is outside the cycle.
(2) Phase one: find a meeting point inside the cycle. We loop until the two pointers meet. There is always a cycle, so the walk never runs off the end. That is why there is no fast and fast.next check like the linked-list version had. In TypeScript, a do ... while loop runs its body once before checking, which matches Python's while True with a break.
(3) slow takes one step: follow the signpost at its position.
(4) fast takes two steps: follow the signpost, then follow the signpost there.
(5) Stop when they meet. They are now on the same position somewhere inside the cycle, not necessarily at its start.
(6) Phase two: start a second slow pointer at position 0.
(7) Move both one step at a time until they meet. From the algebra in 4.9.7, the distance from the start to the cycle's entrance equals the distance from the meeting point to the entrance, plus some whole number of laps. So after the same number of steps, both pointers arrive at the entrance together.
Time. In phase one, slow reaches the cycle in at most n steps, and fast catches it within at most another n steps, for the same reason as in 4.9.7. In phase two, both pointers reach the entrance in at most n steps. That is at most about 3n steps, which is O(n).
Space. Three pointer numbers. The list is never changed. So the extra memory is O(1).
This is the only common solution that meets all three requirements: linear time, constant memory, and no changes to the list.
There is a second solution, and it is much easier to come up with under pressure.
Instead of searching the list, search the range of possible answers, 1 to n. Pick a middle value m, and count how many numbers in the list are at mostm.
If there were no repeat at or below m, exactly m numbers would be at most m: one each of 1, 2, … , m. So:
If the count is more thanm, some value at or below m must be repeated. Search the lower half.
Otherwise, the repeat is above m. Search the upper half.
python
def findDuplicate(self, nums): low, high = 1, len(nums) - 1 while low < high: mid = (low + high) // 2 count = sum(1 for x in nums if x <= mid) # how many values are ≤ mid if count > mid: high = mid # the repeat is in [low, mid] else: low = mid + 1 # the repeat is in [mid+1, high] return low
Each round counts the whole list, O(n), and halves the range, so there are about \log n rounds. That is O(n \log n) time and O(1) memory, and it does not change the list either. It is slower than the cycle method but meets the rules. This is binary search on the answer, which gets its own chapter in 4.11.
If the cycle idea does not come to you in an interview, offer this one. A correct O(n \log n) answer beats a stalled O(n) one.
If you were allowed to change the list, it becomes easy. You could make nums[abs(x)] negative as a "seen" mark, or swap each value to its home position. Both are O(n) time and O(1) memory.
Missing Number. One value from 0 to n is missing instead of repeated. That has a neat bit trick, in 4.30.5.
The general lesson: when every value is a valid position, you can follow values like pointers, and linked-list techniques start to work on a plain list.
"Why must there be a cycle?"n + 1 positions and only n values. Two positions share a value, so two signposts point to the same place.
"Why is position 0 outside the cycle?" No value is 0, so nothing points to position 0.
"Why is the cycle's entrance the repeated value?" It is the position that two different positions point to, and those two positions hold the same value.
"Is there another way?" Binary search on the answer. Naming both, and the trade-off between them, is the complete answer.
One thing to volunteer: explain why the obvious answers are banned before you solve it. "No extra memory rules out a set. No changes rule out sorting and marking. So the rules are pointing somewhere else." That reasoning is the real skill being tested.
Next:4.9.9 LRU Cache. A design problem, and the clearest example in the book of two data structures covering each other's weakness.